我同意条款CheckBox的isChecked属性

范例说明:

所有的网络服务在User使用之前,都需要签署同意条款,在手机应用程序、手机游戏的设计经验中,常看见CheckBox在同意条款情境的运用,其选取的状态有两种即isChecked=true与isChecked=false。

以下范例将设计一个TextView放入条款文字,在下方配置一个CheckBox Widget作为选取项,通过Button.onClickListener按钮事件处理,取得User同意条款的状态。

当CheckBox.isChecked为true,更改TextView的文字内容为"你已接受同意!!",当未选取CheckBox时,Button则不可以被选择的(被Disabled)。

1、src/com.example.ex04_04/ex04_04.java

利用CheckBox.OnClickListener里的事件来判断Button该不该显示,其方法就是判断Button.Enabled的值;在一开始时,默认参数为false,当有单击CheckBox时,Button参数就修改为true。

package com.example.ex04_04;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class ex04_04 extends Activity {
 private TextView mTextView01;
 private TextView mTextView02;
 private CheckBox mCheckBox01;
 private Button mButton01;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mTextView01=(TextView)findViewById(R.id.TextView01);
        mTextView02=(TextView)findViewById(R.id.TextView02);
        mCheckBox01=(CheckBox)findViewById(R.id.CheckBox01);
        mButton01=(Button)findViewById(R.id.Button01);
       
        /*这里设置和在main.xml里设置初始状态作用一样*/
        /*mCheckBox01默认为未选择状态*/
       // mCheckBox01.setChecked(false);
        /*mButton01默认为不可用状态*/
        //mButton01.setEnabled(false);
       
        mCheckBox01.setOnClickListener(new CheckBox.OnClickListener()
        {
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(mCheckBox01.isChecked()==false)
    {
           /*mButton01设置为不可用状态*/
           mButton01.setEnabled(false);
           mTextView01.setText(R.string.Terms);
           mTextView02.setText(R.string.display);
    }
    else if(mCheckBox01.isChecked()==true)
    {
           /*mButton01设置为可用状态*/
           mButton01.setEnabled(true);
           mTextView02.setText("");
    }
   }
        });
        mButton01.setOnClickListener(new Button.OnClickListener()
        {

   public void onClick(View v) {
    // TODO Auto-generated method stub
    mTextView01.setText(R.string.accept);
          mTextView02.setText("");
   }
        });
    }
}

 

2、res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal|center_vertical"
    >
<TextView 
 android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Terms"
    />
<TextView 
 android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/display"
    />
<CheckBox
 android:text="我同意"
 android:id="@+id/CheckBox01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:checked="false"
 />
<Button
 android:text="确定"
 android:id="@+id/Button01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:enabled="false"
 />
</LinearLayout>

3、res/values/strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="Terms">1、xxxxx/n2、xxxxx/n3、xxxxxxxx/n4、xxxxxxx/n5、xxxxxxx/n6、xxxxxxx/n7、xxxxxxx</string>
    <string name="app_name">ex04_04</string>
   <string name="accept">你已经同意!!</string> 
    <string name="display">请勾选我同意</string>

</resources>

 

posted @ 2013-01-17 14:27  枕头君  阅读(1517)  评论(0编辑  收藏  举报