码迷,mamicode.com
首页 > 移动开发 > 详细

Android 输入控件

时间:2014-11-06 17:42:36      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   color   ar   os   

    今天天气不错 bubuko.com,布布扣  虾米 来讲解 Android中输入的控件  在 Android中输入控件是常见的 随处可见 今天又时间 写一篇Android中输入控件的集合  了解他们的相同处和不同处,下面是Android系统中我们常用到的输入控件 好 废话不多 开始: 
    Android已经为接受来自用户的输入多种不同的输入控件的支持。常见的输入控件包括:

      Buttons
      Text Fields
      Checkboxes
      Radio Buttons
      Spinners
      NumberPicker

   Android 将一个输入控件添加到您的用户界面非常简单 ,将xml元素添加到xml布局.
        
      bubuko.com,布布扣

      Buttons
       Android官方解释:按钮代表一个按钮部件。可以按下按钮,或者点击,由用户来执行一个动作。一个典型的        使用一个活动将下面的按钮:
        <Button
                android:id="@+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
    一个文本字段允许用户输入文本到您的应用程序。它可以是一行或多行。触摸一个文本字段位置,光标,并自动显示键盘。除了打字,文本字段允许各种各样的其他活动,例如文本选择(剪切、复制、粘贴)和数据通过自动完成查找。
    bubuko.com,布布扣

     
<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />
      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:
       
 <EditText
    android:id="@+id/postal_address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/postal_address_hint"
    android:inputType="textPostalAddress|
                       textCapWords|
                       textNoSuggestions" />

    Checkboxs
     checkboxs 允许用户选择一个或多个  通常 checkboxs是一个列表在一个垂直下拉框中
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox android:id="@+id/checkbox_meat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/meat"
        android:onClick="onCheckboxClicked"/>
    <CheckBox android:id="@+id/checkbox_cheese"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheese"
        android:onClick="onCheckboxClicked"/>
</LinearLayout>
       
    在一个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"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</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)引入了另一种切换按钮,称为它提供了一个滑块控件,您可以使用添加开关交换对象。
  
     bubuko.com,布布扣          bubuko.com,布布扣 
     (切换按钮)                     开关 (Android4.0+)
                    
    响应点击事件
当用户选择一个切换按钮和开关,对象收到的点击事件。

要定义Click事件处理程序中,添加机器人:的onClick属性的 <切换按钮>或<开关>元素在XML布局。该属性的值必须是要在响应click事件调用的方法的名称。该活动举办的布局必须再执行相应的方法。


例如,这里有一个切换按钮与安卓的onClick属性:
     
<ToggleButton  
    android:id = "@+id/togglebutton" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" 
    android:textOn = "Vibrate在“ 
    机器人:textOff = “振动关闭” 
    机器人:的onClick = “onToggleClicked” />

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();
    
    if (on) {
        // Enable vibrate
    } else {
        // Disable vibrate
    }
}

    Spinners
     Spinner提供一个快速的方法来选择一个值从一组。在默认状态,微调器显示当前选择的价值。触摸Spinner与所有其他可用值显示一个下拉菜单,用户可以选择的一个新的。
   
    bubuko.com,布布扣
       
     spinner布局

   
     Create a spinner like any view and specify the android:entries attribute to specify the set of options:            
<Spinner
         android:id="@+id/mySpinner"
         android:layout_width="wrap_content"
	 android:entries="@array/planets_arrays"
         android:prompt="@string/planets_prompt"
         android:layout_height="wrap_content" />
    
    特别的string array 的条目在res/values/planets_arrays下
    
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="planets_array">
		<item>Mercury</item>
		<item>Venus</item>
		<item>Earth</item>
		<item>Mars</item>
	</string-array>
</resources>
  
   查看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 layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.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:


  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:

   
<NumberPicker 
        android:id="@+id/np_total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
/>


  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 with setWrapSelectorWheel. If you want to listen as the value changes, we can use the OnValueChangeListener listener:

// within onCreate
numberPicker.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 the NumberPicker docs for more details.

   References
  

 



Android 输入控件

标签:des   android   style   blog   http   io   color   ar   os   

原文地址:http://blog.csdn.net/xin724/article/details/40858261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!