您的位置:首页 > 移动开发 > Android开发

Android入门--CheckBox 的isChecked 属性

2016-06-13 19:37 525 查看
① 创建新工程

② 在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);
}
}
});
}
}





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android CheckBox