android sdk---checkBox的用法实例

MainActivity.java代码:

public CheckBox myCheckBox;
 public TextView myTextView1;
 public TextView myTextView2;
 public Button myButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        myTextView1 = (TextView)findViewById(R.id.myTextView1);
        myTextView2 = (TextView)findViewById(R.id.myTextView2);
        myCheckBox = (CheckBox)findViewById(R.id.checkBox1);
        myButton = (Button)findViewById(R.id.button1);
       
        myCheckBox.setChecked(false);
        myButton.setEnabled(false);
       
        myCheckBox.setOnClickListener(new CheckBox.OnClickListener()
        {
         public void onClick(View v)
         {
          if(myCheckBox.isChecked())
          {
           myButton.setEnabled(true);
           myTextView2.setText("");
          }
          else
          {
           myButton.setEnabled(false);
           myTextView2.setText("");
           myTextView1.setText("please choose");
          }
         }
        });
       
        myButton.setOnClickListener(new Button.OnClickListener()
        {

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(myCheckBox.isChecked())
          {
           myTextView1.setText("wow");
          }
   }
        });

 

activity_main.xml:

<TextView
     android:id="@+id/myTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="28dp"
    android:layout_toRightOf="@+id/myTextView2"
    android:text="CheckBox" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox1"
    android:layout_marginTop="20dp"
    android:text="Button" />

总结如下:

checkBox的函数 :

setChecked

isChecked

setOnClickListener

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值