Android-auto-complete
Android-オートコンプリート
提案を取得したい場合、編集可能なテキストフィールドに入力するときに、AutoCompleteTextViewを介してこれを行うことができます。 ユーザーが入力しているときに自動的に提案を提供します。 候補のリストはドロップダウンメニューに表示され、ユーザーはそこから項目を選択して編集ボックスの内容を置き換えることができます。
AutoCompleteTextViewを使用するには、まずxmlにAutoCompletTextViewフィールドを作成する必要があります。 その構文は次のとおりです。
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10" >
その後、このtextviewの参照をjavaで取得する必要があります。 その構文は次のとおりです。
private AutoCompleteTextView actv;
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
次に行う必要があるのは、表示する提案項目のリストを指定することです。 リストアイテムは、javaまたはstrings.xmlの文字列配列として指定できます。 その構文は次のとおりです。
String[] countries = getResources().getStringArray(R.array.list_of_countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,countries);
actv.setAdapter(adapter);
アレイアダプタークラスは、テキストフィールドの候補ボックスにデータをリストとして表示する役割を果たします。 setAdapter メソッドは、autoCompleteTextViewのアダプターを設定するために使用されます。 これらの方法とは別に、オートコンプリートの他の方法を以下にリストします。
Sr.No | Method & description |
---|---|
1 |
getAdapter() このメソッドは、自動補完に使用されるフィルター可能なリストアダプターを返します |
2 |
getCompletionHint() このメソッドは、一致するリストの下部に表示されるオプションのヒントテキストを返します |
3 |
getDropDownAnchor() このメソッドは、オートコンプリートドロップダウンリストが固定されているビューのIDを返します。 |
4 |
getListSelection() このメソッドは、ドロップダウンビューが選択されている場合、その位置を返します |
5 |
isPopupShowing() このメソッドは、ポップアップメニューが表示されているかどうかを示します |
6 |
setText(CharSequence text, boolean filter) このメソッドは、フィルタリングを無効にできることを除いて、テキストを設定します |
7 |
showDropDown() このメソッドは、画面にドロップダウンを表示します。 |
例
以下の例は、AutoCompleteTextViewクラスの使用方法を示しています。 入力できる基本的なアプリケーションを作成し、デバイスに提案を表示します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android Studio to create an Android application under a package package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add AutoCompleteTextView code |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | Run the application and choose a running android device and install the application on it and verify the results. |
*src/MainActivity.java* のコンテンツは次のとおりです。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
text1=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
ArrayAdapter adapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
text1.setAdapter(adapter);
text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
*activity_main.xml* のコンテンツは次のとおりです。
'_ここでabcはfinddevguidesのロゴについて示しています_
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Auto Complete"
android:id="@+id/textView"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="finddevguides"
android:id="@+id/textView2"
android:textColor="#ff3eff0f"
android:textSize="35dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"/>
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_below="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_marginTop="72dp"
android:hint="AutoComplete TextView">
<requestFocus/>
</AutoCompleteTextView>
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_below="@+id/autoCompleteTextView1"
android:layout_alignLeft="@+id/autoCompleteTextView1"
android:layout_alignStart="@+id/autoCompleteTextView1"
android:hint="Multi Auto Complete "/>
</RelativeLayout>
*Strings.xml* のコンテンツは次のとおりです。
<resources>
<string name="app_name">My Application</string>
</resources>
*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="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.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>
アプリケーションを実行してみましょう。 環境のセットアップ中にAVDを接続したと仮定します。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはこのアプリケーションをAVDにインストールし、AVDは次の画面を表示します。
テキストビューに入力するだけで、言語の候補が表示されます。 as * a *の文字を1つ入力するだけで、言語の提案が表示されます。
multiAutoCompleteTextViewは、単語だけでなくテキスト全体に対する提案を示します。 最初の単語を書いた後、2番目の単語を書き始めると、提案が表示されます。 これは下の図に示されています。