Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 769|回复: 0

Android 基础 UI 编程之五-  Android学习

[复制链接]

该用户从未签到

发表于 2011-10-24 14:50:09 | 显示全部楼层 |阅读模式
RadioButton 单选
RadioGroup 组与onCheckedChanged 事件


①新建工程
②string.xml 中添加字符串
<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">Ex_Ctrl_6</string>

<string name="iam_Boy">帅哥</string>

<string name="iamGirl">美女</string>

<string name="ask">请问你是??</string>

</resources>
复制代码
③修改 mian.xml 布局,添加 UI 元素
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

androidrientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/ask"

android:id="@+id/TextView_Ask_And_Show"

android:textSize="25px"/>

<RadioGroup

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/RadioGroup">

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/RadioButton_Boy"

android:text="@string/iam_Boy"></RadioButton>

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/iamGirl"

android:id="@+id/RadioButton_Gril"></RadioButton>

</RadioGroup>

</LinearLayout>
复制代码
④修改 mainActivity.java 文件
package zyf.Ex_Ctrl_6;

import

import

import

import

import

android.app.Activity;

android.os.Bundle;

android.widget.RadioButton;

android.widget.RadioGroup;

android.widget.TextView;

public class Ex_Ctrl_6 extends Activity {

/** Called when the activity is first created. */

private TextView answer_TextView;

private RadioButton boy_RadioButton,girl_RadioButton;

private RadioGroup radioGroup;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/* findViewById()从XML中获取资源对象 */

answer_TextView=(TextView)findViewById(R.id.TextView_Ask_And_Show);

radioGroup=(RadioGroup)findViewById(R.id.RadioGroup);

boy_RadioButton=(RadioButton)findViewById(R.id.RadioButton_Boy);

girl_RadioButton=(RadioButton)findViewById(R.id.RadioButton_Gril);

/*给单RadioGroup添加状态改变监听器*/

radioGroup.setOnCheckedChangeListener(new

RadioGroup.OnCheckedChangeListener(){

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

if(boy_RadioButton.isChecked()){

answer_TextView.setText(R.string.iam_Boy);

}else{

answer_TextView.setText(R.string.iamGirl);

}

}

});

}

}
复制代码
⑤结果








RadioButton 猜猜看
猜猜我是??


①新建项目
②准备三张 png 图片

<resources>
复制代码
④在 main.xml 中添加 UI 元素
<?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"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/ask"

android:id="@+id/TextView_Ask_And_Show"

android:textSize="25px"/>



<RadioGroup

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/RadioGroup">

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/RadioButton_Boy"

android:text="@string/iam_Boy">

</RadioButton>

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/iamGirl"

android:id="@+id/RadioButton_Gril">

</RadioButton>

</RadioGroup>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<Button

android:id="@+id/answer_The_Q_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/answer_Q"

></Button>

<Button

android:id="@+id/clean_The_Q_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/clean"

></Button>

</LinearLayout>

</LinearLayout>
复制代码

⑤修改 mainActivity.java 文件
package zyf.Ex_Ctrl_6;

import

import

import

import

import

java.util.Random;

android.app.Activity;

android.app.AlertDialog;

android.os.Bundle;

android.view.Menu;

import

import

import

import

import

import

import

android.view.MenuItem;

android.view.View;

android.widget.Button;

android.widget.RadioButton;

android.widget.RadioGroup;

android.widget.TextView;

android.widget.Toast;

public class Ex_Ctrl_6 extends Activity implements Button.OnClickListener {

/** Called when the activity is first created. */

private TextView answer_TextView;

private RadioButton boy_RadioButton, girl_RadioButton;

private RadioGroup radioGroup;

private Button answer_Button, clean_Button;

private String[] boy_girl;

private AlertDialog.Builder showRightorNot;

private MenuItem exit;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/* 结果显示对话框 */

showRightorNot = new AlertDialog.Builder(this);

/* findViewById()从XML中获取资源对象 */

answer_TextView = (TextView) findViewById(R.id.TextView_Ask_And_Show);

radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);

boy_RadioButton = (RadioButton) findViewById(R.id.RadioButton_Boy);

girl_RadioButton = (RadioButton) findViewById(R.id.RadioButton_Gril);

answer_Button = (Button) findViewById(R.id.answer_The_Q_button);

clean_Button = (Button) findViewById(R.id.clean_The_Q_button);

/* 答案数组 */

boy_girl = new String[] { "Boy", "Girl","Boy", "Girl",

"Boy", "Girl","Boy", "Girl",

"Boy" };

/* 按钮设置成不可选 */

answer_Button.setEnabled(false);

clean_Button.setEnabled(false);

/* 给单RadioGroup添加状态改变监听器 */

radioGroup.setOnCheckedChangeListener(new

RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group,int checkedId){

// TODO Auto-generated method stub

/* 判断显示 */

if (boy_RadioButton.isChecked()) {

answer_TextView.setText(R.string.iam_Boy);

} else {

answer_TextView.setText(R.string.iamGirl);

}

}

});

boy_RadioButton.setOnClickListener(new RadioGroup.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

/* 按钮设置成可选 */

answer_Button.setEnabled(true);

clean_Button.setEnabled(true);

}

});

girl_RadioButton.setOnClickListener(new RadioGroup.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

/* 按钮设置成可选 */

answer_Button.setEnabled(true);

clean_Button.setEnabled(true);

}

});

/* 设置按钮事件监听器 */

answer_Button.setOnClickListener(this);

clean_Button.setOnClickListener(this);

}



@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if (v.getId() == R.id.answer_The_Q_button) {

if (boy_RadioButton.isChecked()) {

checkTheAnswer("Boy");



}



else if (girl_RadioButton.isChecked()) {



checkTheAnswer("Girl");

}

}

if (v.getId() == R.id.clean_The_Q_button) {

/* 按钮设置成未选取 */

boy_RadioButton.setChecked(false);

girl_RadioButton.setChecked(false);





/* 按钮设置成不可选 */

answer_Button.setEnabled(false);

clean_Button.setEnabled(false);

answer_TextView.setText(R.string.ask);





}

}





private void checkTheAnswer(String checkstring) {

// TODO Auto-generated method stub

/*检测提示*/

Toast.makeText(this, "检测答案…………", Toast.LENGTH_SHORT).show();

/*获取随机数*/

Random random = new Random();

int index = random.nextInt(9);





if (boy_girl[index].equals(checkstring)) {

/*回答正确 ,设置提示对话框,显示结果*/

showRightorNot.setIcon(R.drawable.right);

showRightorNot.setTitle(R.string.showAnswerTitle_YES);

showRightorNot.setPositiveButton(R.string.about_dialog_ok, null);

showRightorNot.setMessage(R.string.right).show();

Toast.makeText(this,getString(R.string.answerIS)

+boy_girl[index],Toast.LENGTH_LONG).show();

} else {

/*回答错误 ,设置提示对话框,显示结果*/

showRightorNot.setIcon(R.drawable.wrong);

showRightorNot.setTitle(R.string.showAnswerTitle_NO);

showRightorNot.setPositiveButton(R.string.about_dialog_ok, null);



showRightorNot.setMessage(R.string.wrong).show();





Toast.makeText(this,getString(R.string.answerIS)+

boy_girl[index], Toast.LENGTH_LONG).show();

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub

/*添加退出菜单*/

exit=menu.add("Exit");

/*设置退出菜单图片*/

exit.setIcon(R.drawable.ic_menu_close_clear_cancel);

return super.onCreateOptionsMenu(menu);

}





@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

/*结束Activity*/

finish();

return super.onOptionsItemSelected(item);

}

}
复制代码结果:
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-11 11:43 , Processed in 0.356456 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表