首页 > 代码库 > AutoCompleteTextView和Spinner的使用方法
AutoCompleteTextView和Spinner的使用方法
首先说明AutoCompleteTextView的使用方法。主要是起到自己主动填充的作用。输入keyword,假设存在该字便能够自己主动填充。代码例如以下
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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <AutoCompleteTextView android:id="@+id/autotextview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionHint="请选择喜欢的图书" android:textColor="#000000" android:completionThreshold="1" /> <Button android:id="@+id/btn_spinner" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Spinner 界面"/> </RelativeLayout>MianActivity.java
package lzl.edu.com.autocomplete; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; public class MainActivity extends Activity { private AutoCompleteTextView autoTextView; private Button btn_spinner; String[] books = {"从你的全世界路过","荆棘鸟","老人与海","第一行代码","我们从未陌生过"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoTextView = (AutoCompleteTextView)findViewById(R.id.autotextview); btn_spinner = (Button)findViewById(R.id.btn_spinner); // android.R.layout.simple_dropdown_item_1line 系统提供的自己主动填充的方法。 ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,books); autoTextView.setAdapter(aa); btn_spinner.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,BookeSpinner.class); startActivity(intent); } }); } }
Spinner通常和arrays.xml结合集合使用。在value目录下创建arrays.xml文件。
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="books"> <item>从你的全世界路过</item> <item>你好,世界</item> <item>JAVA疯狂讲义</item> <item>第一行代码</item> <item>荆棘鸟</item> </string-array> </resources>在xml文件里建立Spinner标签
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="lzl.edu.com.autocomplete.BookeSpinner"> <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/books" > </Spinner> </RelativeLayout>Activity中增加布局就能够了
package lzl.edu.com.autocomplete; import android.app.Activity; import android.os.Bundle; public class BookeSpinner extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_booke_spinner); } }Spinnner结合Adapter的使用
BooksActivity.java
package lzl.edu.com.autocomplete; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Spinner; import android.widget.TextView; public class BookeSpinner extends Activity { Spinner spinnerbooks; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_booke_spinner); spinnerbooks = (Spinner)findViewById(R.id.spinnerbooks); BaseAdapter ba = new BaseAdapter() { @Override public int getCount() { return 100; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = new TextView(BookeSpinner.this); textView.setText("第"+position+"本书"); textView.setTextColor(Color.rgb(240, 200, 100)); textView.setTextSize(20); return textView; } }; spinnerbooks.setAdapter(ba); } }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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="lzl.edu.com.autocomplete.BookeSpinner"> <Spinner android:id="@+id/spinnerbooks" android:layout_width="fill_parent" android:layout_height="wrap_content" > </Spinner> </RelativeLayout>
AutoCompleteTextView和Spinner的使用方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。