一.Spinner(下拉列表):
编码实现:先要在布局文件中定义Spinner组件,然后将可选内容通过ArrayApadter和下拉列表连接起来,最后要获得用户选择选项,要设计事件监听setOnItemSelectedListener并实现其中的onItemSelected,从而获得用户所选择的内容。
其中ArrayAdapter适配器,它的作用是连接数据源与视图,也就是可以将数组里面的数据,在spinnner中显示。
实例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /></LinearLayout>
代码部分:
public class MainActivity extends Activity { public static final String[] m_Countries = {"O型","A型","B型","AB型","其他"}; private TextView textView; private Spinner spinner; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView1); spinner = (Spinner)findViewById(R.id.spinner1); //将可选内容与ArrayAdapter连接 adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m_Countries); //设置下拉列表风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter添加到spinner中 spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { displayToast("你选中了"+m_Countries[arg2]); textView.setText("你的血型是:"+m_Countries[arg2]); //设置显示当前选择的项 arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } public void displayToast(String text){ Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); } @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; }}
运行界面:
当选中血型之后会有提示信息,然后将TextView中显示内容改变为选中类型。
二.AutoCompleteTextView和MultiAutoCompleteTextView(自动提示):
AutoCompleteTextView:支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据。
MultiAutoCompleteTextView:可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配,例如短信发送时,联系人的添加。
布局实现:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <AutoCompleteTextView android:id="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </MultiAutoCompleteTextView> </LinearLayout>
代码实现:
public class MainActivity extends Activity { public static final String[] auto = {"tiger","bull", "horse","elephant","dog","pig","duck","bee","cat","dove","eagle","fish","whale"}; private TextView textView; private ArrayAdapter<String> adapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView1); //关联关键字 adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,auto); AutoCompleteTextView autoView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); //将adapter添加到AutoCompleteTextView中。 autoView.setAdapter(adapter); MultiAutoCompleteTextView multView = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1); //将adapter添加到MultiAutoCompleteTextView中。 multView.setAdapter(adapter); multView.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.main, menu); return true; }}
运行图片:
AutoCompleteTextView:
MultiAutoCompleteTextView:
两着都可以通过setThreshold()方法来设置输入多少字符开始检索。其默认为2个字符。
三.DatePicker、TimePicker(日期和时间):
android中用DatePicker来实现日期,TimePicker来实现时间。
实现过程:
现在布局文件中定义DatePicker和TimePicker,然后通过Calendar类获得系统时间,接着通过init()方法将日期传递给DatePicker,并设置OnDateChangedListener来监听日期改变,当时间被改变时需要设置setOnTimeChangedListener()监听来设置时间。
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日期时间设置" /> <DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#228b22" /> <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
代码实现:
public class MainActivity extends Activity { private TextView textview; private DatePicker datepicker; private TimePicker timepicker; //java中的Calendar Calendar c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); c = Calendar.getInstance(); textview = (TextView)findViewById(R.id.textView1); datepicker = (DatePicker)findViewById(R.id.datePicker1); //将日记初始化为当前系统时间,并设置起监听事件 datepicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //当提起更改时,在这里处理。 } }); timepicker = (TimePicker)findViewById(R.id.timePicker1); //设置为24小时制显示 timepicker.setIs24HourView(true); timepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { //时间改变时处理。自己可添加 } }); } @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; }}
运行图片: