Android 控件的一些常用的方法和属性

CheckBox

CheckBox和RadioBox都是从CompoundButton中继承的,而CompoundButton是继承TextView。在XML中如下定义:

  <CheckBox
      android:id="@+id/mycheckbox"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="This checkbox is:uncheck"
      />

常用的checkcbox函数有:isChecked(),setChecked(),toggle()(改变状态,如是checked的变成unchecked,如果是unchecked变为checked。)。如果CheckBox的状态发生更改,需要在程序中进行触发方法处理。如下:

public class HelloAndriod extends Activity implements CompoundButton.OnCheckedChangeListener{

    private CheckBox mycheckbox = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        ... ...
        mycheckbox = (CheckBox)findViewById(R.id.mycheckbox);
        mycheckbox.setOnCheckedChangeListener(this);
    }
    
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
           mycheckbox.setText("This checkbox is : " + (isChecked ? "checked":"unchcked"));
    }
}

RadioBox

RadioBox处理外观上于CheckBox不一样外,RadioBox通常会组成一个Group,在Group中只能有一个RadioBox处于checked状态。在XML中如下处理:

  <RadioGroup
      android:id="@+id/myradiogroup"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" >
        <RadioButton android:id="@+id/radio1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text ="Radio Text One" />
        <RadioButton android:id="@+id/radio2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text ="Radio Text Two" />
        <RadioButton android:id="@+id/radio3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text ="Radio Text Three" />
  </RadioGroup>

我们更常操作RadioGroup,常见的方法包括有check(),例如roup.check(R.id.radio1)),将R.id.radio1对应的radiobutton选上;clearCheck(),清楚所有的选择,全部都是unchecked;getCheckedRadioButtonId(),获取选上的radiobutton的ID,无则返回-1。在下面的例子中,我们在之前的checkbox的例子上增加radiobox

public class HelloAndriod extends Activity  implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener{
    private RadioGroup myradiogroup = null;
    
    public void onCreate(Bundle savedInstanceState){
        ... ...
        myradiogroup = (RadioGroup)findViewById(R.id.myradiogroup);
        myradiogroup.setOnCheckedChangeListener(this);
    }
    
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
           mycheckbox.setText("This checkbox is : " + (isChecked ? "checked":"unchcked"));
    }

    public void onCheckedChanged(RadioGroup  group, int checkedId){
           int radioId = myradiogroup.getCheckedRadioButtonId();
           if(radioId < 0 )
               myTextView.setText("No Radio Button is selected");
           else{
               RadioButton rb = (RadioButton)group.findViewById(radioId);
               myTextView.setText("radio button: " + rb.getText());
           }
     }
}

我们在res/layout/目录下新增一个Android XML文件button_color.xml对我们上面的button在不同状态下的颜色进行描述:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00ff00" android:state_focused="true"/>
    <item android:color="#ff0000" android:state_pressed="true" />
    <item android:color="#0000ff" android:state_pressed="false" />
</selector>

这些选择从上至下是具有优先级别的,例如我们将state_focused放置在最后,并不起作用,因为会先执行了state_pressed="false"的颜色。相关的状态有state_pressed, button_color, state_focused, state_selected, state_active, state_checkable, state_checked, state_enabled, state_window_focused。

然后我们main.xml,对相关的widget,增加:android:textColor="@layout/button_color"

posted @ 2015-11-20 10:54  醉孟子  阅读(217)  评论(0)    收藏  举报