我同意条款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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库