Kita mulai saja, buat project Android baru di Eclipse. Pada MainActivity.java kita buat kode program seperti ini :
package com.example.multiautocompletetextview_android;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
//http://cariprogram.blogspot.com
//nuramijaya@gmail.com
public class MainActivity extends Activity {
TextWatcher tw;
MultiAutoCompleteTextView multiAutoCompleteTextView1;
String daftar[] = {"Google", "IBM", "Apple",
"Samsung", "Blackberry", "Microsoft", "Hewlet Packard", "Dell",
"Acer", "Lenovo", "Nokia", "Yahoo", "Amazon",
"Facebook", "Asus", "HTC", "Ericsson", "Sony",
"eBay", "Alibaba"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme); //TAMBAHKAN BARIS INI KARENA BUG AUTOCOMPLETETEXTVIEW DI 2.3 TIDAK KELIHATAN KARENA TEXT BERWARNA PUTIH
setContentView(R.layout.activity_main);
tw = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
multiAutoCompleteTextView1 = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
multiAutoCompleteTextView1.addTextChangedListener(tw);
multiAutoCompleteTextView1.setThreshold(1);
multiAutoCompleteTextView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, daftar));
multiAutoCompleteTextView1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Kemudian tampilannya ada di activity_main.xml, buatlah seperti ini :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </MultiAutoCompleteTextView> </LinearLayout>
Untuk AndroidManifest.xml tidak perlu diubah, seperti defaultnya di bawah ini :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.multiautocompletetextview_android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.multiautocompletetextview_android.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>
Hasilnya :
Project selengkapnya silakan download disini. Jika kesulitan bisa melihat cara downloadnya seperti ini.
Semoga berhasil :)
Tidak ada komentar:
Posting Komentar