今天天气不错虾米 来讲解 Android中输入的控件 在 Android中输入控件是常见的 随处可见 今天又时间 写一篇Android中输入控件的集合 了解他们的相同处和不同处,下面是Android系统中我们常用到的输入控件 好 废话不多 开始:
Android已经为接受来自用户的输入多种不同的输入控件的支持,
Android 将一个输入控件添加到您的用户界面非常简单 ,将xml元素添加到xml布局.
Buttonsvc3Ryb25nPgo8c3Ryb25nPiAgICAgICBBbmRyb2lkudm3vb3iys2jurC0xaW0+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:Checkboxscheckboxs 允许用户选择一个或多个 通常 checkboxs是一个列表在一个垂直下拉框中<?xml version="1.0" encoding="utf-8"?>
在一个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内它们分组在一起。通过分组在一起,可以选择系统确保只有一个单选按钮。<?xml version="1.0" encoding="utf-8"?>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)引入了另一种切换按钮,称为它提供了一个滑块控件,您可以使用添加开关交换对象,Android 输入控件》(https://www.unjs.com)。(切换按钮) 开关 (Android4.0+)响应点击事件
当用户选择一个切换按钮和开关,对象收到的点击事件。
要定义Click事件处理程序中,添加机器人:的onClick属性的或元素在XML布局。该属性的值必须是要在响应click事件调用的方法的名称。该活动举办的布局必须再执行相应的方法。
例如,这里有一个切换按钮与安卓的onClick属性:特别的string array 的条目在res/values/planets_arrays下<?xml version="1.0" encoding="utf-8"?>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 layoutArrayAdapteradapter = 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 DialogNote that we can also use a ListView in this case instead to avoid a spinner altogether.NumberPickerThis 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.
Referenceshttp://developer.android.com/guide/topics/ui/controls.html