Android入门--CheckBox 的isChecked 属性
2016-06-13 19:37
525 查看
① 创建新工程
② 在string.xml 中添加字符串
② 在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); } } }); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories