Android入门--CheckBox 的isChecked 属性

① 创建新工程

② 在string.xml 中添加字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Forward</string>
    <string name="advice">请勾选我同意</string>
    <string name="accept">您已接受同意!!</string>
    <string name="Notaccept">您未同意!!</string>
    <string name="allOK">全部许可</string>
    <string name="OKs">确定</string>

</resources>
布局文件activity_forward.xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hanji.forward.Forward" >

    <TextView
        android:id="@+id/TextView_Guide"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/advice"
        android:textSize="28px" >
    </TextView>

    <CheckBox
        android:id="@+id/CheckBox_Accept"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView_Guide"
        android:layout_marginTop="20dp"
        android:text="@string/allOK" />

    <TextView
        android:id="@+id/TextView_youChoiceShow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/CheckBox_Accept"
        android:layout_toLeftOf="@+id/CheckBox_Accept"
        android:layout_marginLeft="20dp"
        android:textSize="25px" />
    
    <Button
        android:id="@+id/Button_OK"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/CheckBox_Accept"
        android:layout_marginTop="20dp"
        android:text="@string/OKs" />

</RelativeLayout>
Forward.java代码如下:
package com.hanji.forward;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class Forward extends ActionBarActivity {

	private TextView showAdvice, yourChoiceshow;
	private CheckBox iAccept;
	private Button ok;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forward);

		/* findViewById()从资源ID获取资源对象 */
		showAdvice = (TextView) findViewById(R.id.TextView_Guide);
		yourChoiceshow = (TextView) findViewById(R.id.TextView_youChoiceShow);
		iAccept = (CheckBox) findViewById(R.id.CheckBox_Accept);
		ok = (Button) findViewById(R.id.Button_OK);
		/* 获取XML中字符串 */
		CharSequence titleString = getString(R.string.allOK);
		/* 设置复选框标题 */
		iAccept.setHint(titleString);
		/* 设置复选框标题字体颜色 */
		iAccept.setHintTextColor(Color.RED);
		/* 将CheckBox设置成未选中 */
		iAccept.setChecked(false);
		/* 将Button设置成不可选 */
		ok.setEnabled(false);
		iAccept.setOnClickListener(new CheckBox.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (iAccept.isChecked()) {
					ok.setEnabled(true);
					yourChoiceshow.setText(R.string.accept);
				} else {
					ok.setEnabled(false);
					yourChoiceshow.setText(R.string.Notaccept);
				}
			}
		});
		ok.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (iAccept.isChecked()) {
					showAdvice.setText(R.string.accept);
				}
			}
		});
	}
}
       








  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值