Thứ Tư, 21 tháng 11, 2018

Android - Trình kiểm tra chính tả

Nền tảng Khóa học lập trình Android cung cấp một khung kiểm tra chính tả cho phép bạn triển khai và truy cập kiểm tra chính tả trong ứng dụng của bạn.

Để sử dụng kiểm tra chính tả, bạn cần triển khai giao diện SpellCheckerSessionListener và ghi đè các phương thức của nó. Cú pháp của nó được đưa ra dưới đây
public class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener {
   @Override
   public void onGetSuggestions(final SuggestionsInfo[] arg0) {
      // TODO Auto-generated method stub
   }
   
   @Override
   public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
      // TODO Auto-generated method stub
   }
}
Khóa học lập trình Android Điều tiếp theo bạn cần làm là tạo một đối tượng của lớp SpellCheckerSession .

Đối tượng này có thể được khởi tạo bằng cách gọi phương thức newSpellCheckerSession của lớp TextServicesManager.

Học lập trình Android

Lớp này xử lý sự tương tác giữa các ứng dụng và các dịch vụ văn bản. Bạn cần yêu cầu dịch vụ hệ thống để khởi tạo nó. Cú pháp của nó được đưa ra dưới đây

private SpellCheckerSession mScs;
final TextServicesManager tsm = (TextServicesManager) getSystemService(
Context.TEXT_SERVICES_MANAGER_SERVICE);
mScs = tsm.newSpellCheckerSession(null, null, this, true);   

Khóa học lập trình Android Điều cuối cùng bạn cần làm là gọi phương thức getSuggestions để nhận đề xuất cho bất kỳ văn bản nào bạn muốn.

Các đề xuất sẽ được chuyển vào phương pháp onGetSuggestions từ nơi bạn có thể làm bất cứ điều gì bạn muốn.
mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3);
Phương thức này có hai tham số. Tham số đầu tiên là chuỗi dưới dạng đối tượng Thông tin văn bản và thông số thứ hai là số cookie được sử dụng để phân biệt các đề xuất.

Ngoài các phương thức, có các phương thức khác được cung cấp bởi lớp SpellCheckerSession để có các đề xuất xử lý tốt hơn. Những phương pháp này được liệt kê dưới đây

Sr.NoPhương pháp & mô tả
1hủy ()

Hủy tác vụ kiểm tra chính tả đang chờ xử lý và đang chạy
2gần()

Kết thúc phiên này và cho phép TextServicesManagerService ngắt kết nối trình kiểm tra chính tả bị ràng buộc
3getSentenceSuggestions (TextInfo [] textInfos, int suggestionsLimit)

Nhận đề xuất từ ​​các câu được chỉ định
4getSpellChecker ()
Nhận thông tin dịch vụ trình kiểm tra chính tả mà phiên kiểm tra chính tả này có.
5isSessionDisconnected ()

Đúng nếu kết nối với dịch vụ văn bản của phiên này bị ngắt kết nối và không hoạt động.

Thí dụ

Khóa học lập trình Android Dưới đây là ví dụ minh họa việc sử dụng Trình kiểm tra chính tả. Nó tạo ra một ứng dụng kiểm tra chính tả cơ bản cho phép bạn viết văn bản và nhận các đề xuất.

Để thử nghiệm với ví dụ này, bạn có thể chạy nó trên một thiết bị thực tế hoặc trong một trình giả lập.

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết.
3Sửa đổi res / layout / main để thêm các thành phần XML tương ứng
4Chạy ứng dụng và chọn thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả
Sau đây là nội dung của tệp hoạt động chính đã sửa đổi src / MainActivity.java .
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;

import android.widget.Button;
import android.widget.EditText;

import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
import android.view.textservice.SuggestionsInfo;

import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SpellCheckerSessionListener  {
   Button b1;
   TextView tv1;
   EditText ed1;
   private SpellCheckerSession mScs;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1=(Button)findViewById(R.id.button);
      tv1=(TextView)findViewById(R.id.textView3);

      ed1=(EditText)findViewById(R.id.editText);
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(),
               ed1.getText().toString(),Toast.LENGTH_SHORT).show();
            mScs.getSuggestions(new TextInfo(ed1.getText().toString()), 3);
         }
      });
   }

   public void onResume() {
      super.onResume();
      final TextServicesManager tsm = (TextServicesManager)
         getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
      mScs = tsm.newSpellCheckerSession(null, null, this, true);
   }

   public void onPause() {
      super.onPause();
      if (mScs != null) {
         mScs.close();
      }
   }

   public void onGetSuggestions(final SuggestionsInfo[] arg0) {
      final StringBuilder sb = new StringBuilder();

      for (int i = 0; i < arg0.length; ++i) {
         // Returned suggestions are contained in SuggestionsInfo
         final int len = arg0[i].getSuggestionsCount();
         sb.append('\n');

         for (int j = 0; j < len; ++j) {
            sb.append("," + arg0[i].getSuggestionAt(j));
         }

         sb.append(" (" + len + ")");
      }
  
      runOnUiThread(new Runnable() {
         public void run() {
            tv1.append(sb.toString());
         }
      });
   }

   @Override
   public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
      // TODO Auto-generated method stub
   }
}
Sau đây là nội dung sửa đổi của xml res / layout / main.xml .
Trong đoạn mã sau abc cho biết logo của tutorialspoint.com
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView android:text="Spell checker " android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Suggestions"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:hint="Enter Text"
      android:layout_above="@+id/button"
      android:layout_marginBottom="56dp"
      android:focusable="true"
      android:textColorHighlight="#ff7eff15"
      android:textColorHint="#ffff25e6"
      android:layout_alignRight="@+id/textview"
      android:layout_alignEnd="@+id/textview"
      android:layout_alignLeft="@+id/textview"
      android:layout_alignStart="@+id/textview" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Suggestions"
      android:id="@+id/textView3"
      android:textSize="25sp"
      android:layout_below="@+id/imageView" />

</RelativeLayout>
Sau đây là nội dung của res / values ​​/ string.xml .
<resources>
   <string name="app_name">My Application</string>
</resources>
Sau đây là nội dung của tệp AndroidManifest.xml .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>
Hãy thử chạy ứng dụng ourr chúng ta vừa sửa đổi. Tôi cho rằng bạn đã tạo AVD của mình trong khi thiết lập môi trường.

Để chạy ứng dụng từ Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. 

Android studio cài đặt ứng dụng trên AVD của bạn và khởi động ứng dụng và nếu mọi thứ đều ổn với thiết lập và ứng dụng của bạn, ứng dụng sẽ hiển thị cửa sổ Trình mô phỏng sau

Khóa học lập trình Android Bây giờ những gì bạn cần làm là nhập bất kỳ văn bản nào trong trường. Ví dụ, tôi đã nhập một số văn bản. Nhấn nút đề xuất. Thông báo sau sẽ xuất hiện trong AVD cùng với các đề xuất

Bây giờ thay đổi văn bản và nhấn nút một lần nữa, như tôi đã làm. Và đây là những gì xuất hiện trên màn hình.

Không có nhận xét nào:

Đăng nhận xét

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...