Activity_main.xml
<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" >
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10">
<requestFocus />
</MultiAutoCompleteTextView>
</RelativeLayout>
MainActivity.java
packagecom.swamys.multiautocompletetextview;
importandroid.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
importandroid.widget.Toast;
public class MainActivity extends Activity {
MultiAutoCompleteTextViewmultiAutoCompleteTextView;
String months[] = { "jan", "feb", "march", "aprl", "may", "jun", "july" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, months);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
multiAutoCompleteTextView
.setOnItemClickListener(newOnItemClickListener() {
@Override
public voidonItemClick(AdapterView<?> adapterView,
View view, int position, long arg3) {
Toast.makeText(
getApplicationContext(),
adapterView.getItemAtPosition(position)
.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
Komentar
Posting Komentar