android基礎入門控件詳解(5)

一.Spinner(下拉列表):

編碼實現:先要在佈局文件中定義Spinner組件,然後將可選內容通過ArrayApadter和下拉列表連接起來,最後要獲得用戶選擇選項,要設計事件監聽setOnItemSelectedListener並實現其中的onItemSelected,從而獲得用戶所選擇的內容。

其中ArrayAdapter適配器,它的作用是連接數據源與視圖,也就是可以將數組裡面的數據,在spinnner中顯示。

實例:


    
    

代碼部分:

public class MainActivity extends Activity {

	public static final String[] m_Countries = {"O型","A型","B型","AB型","其他"};
	private TextView textView;
	private Spinner spinner;
	private ArrayAdapter  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(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:可支持選擇多個值(在多次輸入的情況下),分別用分隔符分開,並且在每個值選中的時候再次輸入值時會自動去匹配,例如短信發送時,聯系人的添加。

佈局實現:


    

    

    
    
   

代碼實現:

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 adapter ;
 		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textView = (TextView)findViewById(R.id.textView1);
	
		//關聯關鍵字
		adapter = new  ArrayAdapter(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()監聽來設置時間。

佈局文件:


    

    

    


代碼實現:

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;
	}
}

運行圖片:




發佈留言