您的位置:首页 > 其它

AutoCompleteTextView的使用

2013-06-14 23:34 274 查看
public class MainActivity extends Activity implements OnClickListener {

 private static String[] str = new String[]{

   "Android","Android Blog","Android Market","Android SDK","Android AVD"};

 @SuppressWarnings("unused")

 private TextView show;

 private AutoCompleteTextView autoTextView;

 private Button clean;

 private ArrayAdapter<String> arrayAdapter;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        show = (TextView)this.findViewById(R.id.textView1);

       

//        List<String> list = new ArrayList<String>();

//        for(int i =0;i<str.length;i++){

//         

//         list.add(str[i]);

//        }

        autoTextView = (AutoCompleteTextView)this.findViewById(R.id.autoCompleteTextView1);

        clean = (Button)this.findViewById(R.id.button1);

        //实现一个适配器对象,用来给自动完成输入框添加自动装入的内容

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

        //给自动完成输入框添加内容适配器

        autoTextView.setAdapter(arrayAdapter);

        clean.setOnClickListener(this);

    }

   

    @Override

    public void onClick(View v) {

     //清空

     autoTextView.setText("");

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

   

}

 

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" >

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:text="@string/input" />

    <AutoCompleteTextView

        android:id="@+id/autoCompleteTextView1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_below="@+id/textView1"

        android:hint="Please enter information"

        android:layout_marginTop="17dp"

        android:ems="10" />

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/autoCompleteTextView1"

        android:layout_below="@+id/autoCompleteTextView1"

        android:text="@string/btn" />

</RelativeLayout>

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: