Android 輸入控制項

來源:互聯網
上載者:User

Android 輸入控制項
今天天氣不錯 蝦米 來講解 Android中輸入的控制項 在 Android中輸入控制項是常見的 隨處可見 今天又時間 寫一篇Android中輸入控制項的集合 瞭解他們的相同處和不同處,下面是Android系統中我們常用到的輸入控制項 好 廢話不多 開始:
Android已經為接受來自使用者的輸入多種不同的輸入控制項的支援。常見的輸入控制項包括: Buttons Text Fields Checkboxes Radio Buttons Toggle Buttons Spinners NumberPicker Date and Time Pickers
Android 將一個輸入控制項添加到您的使用者介面非常簡單 ,將xml元素添加到xml布局.
Buttons<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPgo8c3Ryb25nPiAgICAgICBBbmRyb2lkudm3vb3iys2jurC0xaW0+rHt0ru49rC0xaWyv7z+oaO/ydLUsLTPwrC0xaUsu/LV37Xju/cs08nTw7unwLTWtNDQ0ru49rav1/eho9K7uPa15NDNtcQgICAgICAgIMq508PSu7j2u+62r72rz8LD5rXEsLTFpTo8L3N0cm9uZz4KPHN0cm9uZz4gICAgICAgIDxCdXR0b248YnI+CiAgICAgICAgICAgICAgICBhbmRyb2lkOmlkPQ=="@+id/button_id"
android:layout_width="10dp"
android:layout_height="8dp"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/login_input_arrow" />

 public class MyActivity extends Activity {     protected void onCreate(Bundle icicle) {         super.onCreate(icicle);         setContentView(R.layout.content_layout_id);         final Button button = (Button) findViewById(R.id.button_id);         button.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 // Perform action on click             }         });     } }
Text Fields 一個文字欄位允許使用者輸入文本到您的應用程式。它可以是一行或多行。觸摸一個文字欄位位置,游標,並自動顯示鍵盤。除了打字,文字欄位允許各種各樣的其他活動,例如文本選擇(剪下、複製、粘貼)和資料通過自動完成尋找。

 Android:inputType:輸入的類型 允許輸入Text(字串) TextEmailAddress(email的地址) texturi(網址)
 number(數字) phone(號碼) textCansentences() textcapwords() textautocurrect() textpassword() textmultiLine() 

For example, here's how you can collect a postal address, capitalize each word, and disable text suggestions:
 

Checkboxs checkboxs 允許使用者選擇一個或多個 通常 checkboxs是一個列表在一個垂直下拉框中
        
在一個Activity 判斷checkbox是否選中
public void onCheckboxClicked(View view) {    // Is the view now checked?    boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked    switch(view.getId()) {        case R.id.checkbox_meat:            if (checked)                // Put some meat on the sandwich            else                // Remove the meat            break;        case R.id.checkbox_cheese:            if (checked)                // Cheese me            else                // I'm lactose intolerant            break;    }}


Radio Butons 選項按鈕允許使用者選擇一個選項從一組。如果不是必要並排顯示所有選項,使用微調控制項。

建立每個選項按鈕選項,建立一個RadioButton在你的布局。然而,由於選項按鈕是互斥的,你必須RadioGroup內它們分組在一起。通過分組在一起,可以選擇系統確保只有一個選項按鈕。

        



public void onRadioButtonClicked(View view) {    // Is the button now checked?    boolean checked = ((RadioButton) view).isChecked();        // Check which radio button was clicked    switch(view.getId()) {        case R.id.radio_pirates:            if (checked)                // Pirates are the best            break;        case R.id.radio_ninjas:            if (checked)                // Ninjas rule            break;    }}

Toggle Buttons 切換按鈕允許在兩種狀態之間切換設定 您可以添加一個基本的切換按鈕與布局切換按鈕 對象。的Android 4.0(API等級14)引入了另一種切換按鈕,稱為它提供了一個滑塊控制項,您可以使用添加開關交換對象。 (切換按鈕) 開關 (Android4.0+) 響應點擊事件
當使用者選擇一個切換按鈕和開關,對象收到的點擊事件。

要定義Click事件處理常式中,添加機器人:的onClick屬性的 <切換按鈕>或<開關>元素在XML布局。該屬性的值必須是要在響應click事件調用的方法的名稱。該活動舉辦的布局必須再執行相應的方法。


例如,這裡有一個切換按鈕與安卓的onClick屬性:
public void onToggleClicked(View view) {    // Is the toggle on?    boolean on = ((ToggleButton) view).isChecked();        if (on) {        // Enable vibrate    } else {        // Disable vibrate    }}

Spinners Spinner提供一個快速的方法來選擇一個值從一組。在預設狀態,微調器顯示當前選擇的價值。觸摸Spinner與所有其他可用值顯示一個下拉式功能表,使用者可以選擇的一個新的。 spinner布局
Create a spinner like any view and specify the android:entries attribute to specify the set of options:

 
 特別的string array 的條目在res/values/planets_arrays下
 

MercuryVenusEarthMars
查看Spinners指南更多細節。注意,定製一個微調控制項的文本需要使用自訂數組適配器和布局檔案。
擷取和設定多值 String str_spinner=spinner.getseletedItem().toString();
public void setSpinnerToValue(Spinner spinner, String value) {int index = 0;SpinnerAdapter adapter = spinner.getAdapter();for (int i = 0; i < adapter.getCount(); i++) {if (adapter.getItem(i).equals(value)) {index = i;break; // terminate loop}}spinner.setSelection(index);}
自訂ArrayAdapter 資源

Spinner spinner = (Spinner) findViewById(R.id.spinner);// Create an ArrayAdapter using the string array and a default spinner layoutArrayAdapter adapter = ArrayAdapter.createFromResource(this,        R.array.planets_array, android.R.layout.simple_spinner_item);// Specify the layout to use when the list of choices appearsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Apply the adapter to the spinnerspinner.setAdapter(adapter);
Multiple Select Spinner

By default, the spinner only allows the user to select one option from the list. Check out the following resources surrounding multiple selection spinners:

MultiSelectSpinner - Simple multi-select library
MultiSelect Tutorial 1
MultiSelect Tutorial 2
Simple Multi Dialog

Note that we can also use a ListView in this case instead to avoid a spinner altogether.
NumberPicker
This is a widget that enables the user to select a number from a predefined range. First, we put the NumberPicker within the layout XML:



Then we must define the desired range at runtime in the Activity:

public class DemoPickerActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_demo_picker);        NumberPicker numberPicker =             (NumberPicker) findViewById(R.id.np_total);        numberPicker.setMinValue(0);         numberPicker.setMaxValue(100);            numberPicker.setWrapSelectorWheel(true);    }}

Note we set the range with setMinValue and setMaxValue and made the selector wrap withsetWrapSelectorWheel. If you want to listen as the value changes, we can use the OnValueChangeListener listener:
// within onCreatenumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {    @Override    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {        Log.d("DEBUG", "Selected number in picker is " + newVal);    }});


You can also call getValue to retrieve the numeric value any time. See theNumberPicker docs for more details.

References http://developer.android.com/guide/topics/ui/controls.html



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.