読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - AutoCompleTextViewとMultiAutoCompleteTextView

 

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<AutoCompleteTextView
android:id="@+id/single"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="abc"
android:completionThreshold="1"/>

<MultiAutoCompleteTextView
android:id="@+id/multi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"/>

</LinearLayout>
 
MainActivity.java
package com.example.liguofeng.autocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

String[] text = {"abcd", "1234", "ABCD", "あいうえお"};

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

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, text);

AutoCompleteTextView single = (AutoCompleteTextView) findViewById(R.id.single);
single.setAdapter(arrayAdapter);

MultiAutoCompleteTextView multi = (MultiAutoCompleteTextView) findViewById(R.id.multi);
multi.setAdapter(arrayAdapter);
multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

}
}