Android入门--CheckBox 的isChecked 属性 - Go语言中文社区

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








版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/alfany/article/details/51660120
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 16:07:35
  • 阅读 ( 926 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢