Android-autocompletetextview-control
Android-AutoCompleteTextViewコントロール
'_AutoCompleteTextViewは、ユーザーが入力している間に補完候補のリストを自動的に表示することを除いて、EditTextに似たビューです。_
候補のリストがドロップダウンメニューに表示されます。 ユーザーはそこからアイテムを選択して、編集ボックスの内容を置き換えることができます。
AutoCompleteTextViewの属性
以下は、AutoCompleteTextViewコントロールに関連する重要な属性です。 属性の完全なリストと、これらの属性を実行時に変更するために使用できる関連メソッドについては、Androidの公式ドキュメントを確認してください。
Sr.No | Attribute & Description |
---|---|
1 |
android:completionHint これは、ドロップダウンメニューに表示されるヒントを定義します。 |
2 |
android:completionHintView これは、ドロップダウンメニューに表示されるヒントビューを定義します。 |
3 |
android:completionThreshold これは、補完候補がドロップダウンメニューに表示される前にユーザーが入力する必要がある文字数を定義します。 |
4 |
android:dropDownAnchor これは、オートコンプリートドロップダウンを固定するビューです。 |
5 |
android:dropDownHeight これは、ドロップダウンの基本的な高さを指定します。 |
6 |
android:dropDownHorizontalOffset ドロップダウンが水平方向にオフセットされるピクセル量。 |
7 |
android:dropDownSelector これはドロップダウンリストのセレクターです。 |
8 |
android:dropDownVerticalOffset ドロップダウンが垂直方向にオフセットされるピクセル量。 |
9 |
android:dropDownWidth これは、ドロップダウンの基本的な幅を指定します。 |
10 |
android:popupBackground これにより背景が設定されます。 |
例
この例では、簡単な手順を実行して、Linear LayoutとAutoCompleteTextViewを使用して独自のAndroidアプリケーションを作成する方法を示します。
Step | Description |
---|---|
1 | You will use Android Studio IDE to create an Android application and name it as GUIDemo3 under a package com.example.guidemo3 as explained in the Hello World Example chapter. |
2 | Modify src/MainActivity.java file to add a click event. |
3 | Modify the default content of res/layout/activity_main.xml file to include Android UI control. |
4 | Define necessary constants in res/values/strings.xml file |
5 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/com.example.guidemo3/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。
package com.example.guidemo3;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
AutoCompleteTextView autocomplete;
String[] arr = { "Paries,France", "PA,United States","Parana,Brazil",
"Padua,Italy", "Pasadena,CA,United States"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autocomplete = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.select_dialog_item, arr);
autocomplete.setThreshold(2);
autocomplete.setAdapter(adapter);
}
}
以下は res/layout/activity_main.xml ファイルの内容です-
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="@string/example_autocompletetextview"/>
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="54dp"
android:ems="10"/>
</RelativeLayout>
以下は、これらの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GUIDemo3</string>
<string name="example_autocompletetextview">Example showing AutoCompleteTextView<
/string>
</resources>
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guidemo3" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guidemo3.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>
*GUIDemo3* アプリケーションを実行してみましょう。 環境設定中に *AVD* を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Android StudioはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
AutoCompleteTextViewで「pa」を入力すると、次の画面が表示されます-
Android AutoCompleteTextViewコントロール
運動
プログラミング時にレイアウトXMLファイルのAutoCompleteTextViewのさまざまな属性を使用して上記の例を試して、AutoCompleteTextViewのルックアンドフィールを変えることをお勧めします。 編集可能にし、フォントの色、フォントファミリ、幅、textSizeなどに変更して、結果を確認してください。 1つのアクティビティで複数のAutoCompleteTextViewコントロールを使用して上記の例を試すこともできます。