با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن . و کاربرانی که دوره کامل آموزش برنامه نویسی اندروید را دنبال می کنند. پلت فرم آندروید فریم ورک Spelling Checker را ارائه می دهد. این ویژگی به شما اجازه می دهد تا در برنامه خود یک کنترل کننده ی املا را پیاده سازی کرده و از طریق آن غلط های املایی کاربران را مدیریت کنید. برای استفاده از قابلیت های ویژگی spelling checker در اندروید ، شما باید ایتنرفیس SpellCheckerSessionListener را پیاده سازی کرده و متدهای آن را نیز اعمال کنید. برای آشنایی بیشتر با این مباحث و طرز بکارگیری Spelling Checker در اندروید ادامه ی مباحث این بخش را دنبال کنید.
در این مبحث ما شما را با ساختار کلی spelling checker در اندروید و متدهای آن آشنا می کنیم.
همانطور که گفتیم در اولین قدم برای استفاده از spelling checker باید ایتنرفیس SpellCheckerSessionListener را پیاده سازی کنید:
1 2 3 4 5 6 7 8 9 | public class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener { @Override public void onGetSuggestions(final SuggestionsInfo[] arg0) { } @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) { } } |
سپس باید یک آبجکت از کلاس SpellCheckerSession ایجاد کنید.
این شیء را می توان با فراخوانی متد newSpellCheckerSession از کلاس TextServicesManager ایجاد کرد.
این کلاس بین اپلیکیشن و سرویس متن تعامل دارد.
شما نیاز به درخواست system service برای پیدا کردن آن دارید، ساختار که به صورت زیر است:
1 2 3 4 | private SpellCheckerSession mScs; final TextServicesManager tsm = (TextServicesManager) getSystemService( Context.TEXT_SERVICES_MANAGER_SERVICE); mScs = tsm.newSpellCheckerSession(null, null, this, true); |
سپس باید متد getSuggestions را برای ارائه پیشنهادهای املایی متن فراخوانی کنید.
پیشنهادات به متد onGetSuggestions برای مدیریت آن ها منتقل می شوند:
1 | mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3); |
این متد دو پارامتر می گیرد که پارامتر اول رشته در قالب شیء اطلاعات متن است و پارامتر دوم، شماره cookie است که برای تشخیص پیشنهادات استفاده می شود.
علاوه بر متدهای فوق متدهای دیگری نیز از کلاس SpellCheckerSession برای مدیریت بهتر پیشنهادات املایی ارائه شده است که به شرح زیر است:
ردیف | متد و توضیحات مربوطه |
---|---|
۱ | cancel() انتظار تایید را لغو کرده و وظایف spell check را اجرا می کند. |
۲ | close() session جاری را پایان داده و اجازه می دهد که TextServicesManagerService محدودیت spell checker را قطع کند. |
۳ | getSentenceSuggestions(TextInfo[] textInfos, int suggestionsLimit) پیشنهادات را از کلمات و جملات مشخص شده می گیرد. |
۴ | getSpellChecker() اطلاعات سرویس spell checker مربوطه به session جاری را می گیرد. |
۵ | isSessionDisconnected() اگر اتصال به سرویس متن session جاری قطع باشد TRUE را برمی گرداند. |
در این مثال طرز استفاده از Spelling Checker در برنامه نویسی اندروید را بررسی می کنیم.
این مثال یک برنامه ساده است که به کاربر اجازه نوشتن متن و پیشنهادات را می دهد.
شما می توانید این برنامه را روی دستگاه اندروید واقعی و یا شبیه ساز اندروید تست کنید.
مراحل | توضیحات |
---|---|
۱ | مراحل ایجاد برنامه در اندروید استدیو را طبق بخش های قبلی تحت عنوان پکیج com.example.Pvlearn.myapplication دنبال کنید. |
۲ | به محتوای فایل src/MainActivity.java کدهای ضروری را اضافه کنید. |
۳ | به محتوای فایل res/layout/main کمپوننت های xml مربوطه را اضافه کنید. |
۴ | برنامه را اجرا کنید تا دستگاه اندروید شما نتیجه را نمایش دهد. |
محتوای فایل modified main در src/MainActivity.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package com.example.Pvlearn.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 } } |
محتوای فایل res/layout/main.xml:
در کد زیر abc لوگوی pvlearn است.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <?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="Pvlearn" 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> |
محتوای فایل res/values/string.xml:
1 2 3 | resources> <string name="app_name">My Application</string> </resources> |
محتوای فایل AndroidManifest.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.Pvlearn.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> |
برای اجرای برنامه ی اندرویدی خود یکی از فایل های activity در برنامه ی خود کلیک کنید.
سپس روی نماد (Run) از نوار ابزار برای شروع اجرای برنامه کلیک کنید.
صفحه زیر در شبیه ساز اندروید ظاهر خواهد شد:
حالا باید در فیلد text متن مورد نظر را وارد کرده و سپس روی suggestions کلیک کنید تا نتیجه زیر را مشاهده کنید:
حالا متن را تغییر داده و مجددا روی button کلیک کنید تا صفحه زیر را مشاهده کنید:
اندروید به شما اجازه می دهد تا با استفاده از Spelling Checker در برنامه نویسی اندروید غلط های املایی ورودی توسط کاربران را بررسی کرده و پیشنهادات مفید را به جای آن به کاربر ارائه دهید.
در بخش بعدی به بررسی دیتابیس SQLite در اندروید می پردازیم.