Android学习笔记04
【摘要】
[Android开发视频教学].01_12_Android常见控件(二)
主要学习
RadioGroup
RadioButton
CheckBox
Toast的用法
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/r...
[Android开发视频教学].01_12_Android常见控件(二)
主要学习
RadioGroup
RadioButton
CheckBox
Toast的用法
布局文件
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
tools:context=".RadioMain"
android:layout_margin="10dp"/>
<RadioGroup
android:id="@+id/gendergroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/male"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/male" />
<RadioButton
android:id="@+id/female"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<CheckBox
android:id="@+id/up"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/up" />
<CheckBox
android:id="@+id/down"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/down" />
<CheckBox
android:id="@+id/left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/left" />
<CheckBox
android:id="@+id/right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/right" />
</LinearLayout>
<resources>
<string name="app_name">Android07</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_radio_main">RadioMain</string>
<string name="gender">性别</string>
<string name="male">男</string>
<string name="female">女</string>
<string name="up">上</string>
<string name="down">下</string>
<string name="left">左</string>
<string name="right">右</string>
</resources>
Java文件
package fegnda.android07;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioMain extends Activity {
private RadioGroup genderGroup = null;
private RadioButton male = null;
private RadioButton female = null;
private CheckBox up = null;
private CheckBox down = null;
private CheckBox left = null;
private CheckBox right = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_main);
genderGroup = (RadioGroup) findViewById(R.id.gendergroup);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
genderGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (male.getId() == checkedId) {
Toast.makeText(RadioMain.this, "男",
Toast.LENGTH_SHORT).show();
}
if (female.getId() == checkedId) {
Toast.makeText(RadioMain.this, "女",
Toast.LENGTH_SHORT).show();
}
}
});
up = (CheckBox) findViewById(R.id.up);
down = (CheckBox) findViewById(R.id.down);
left = (CheckBox) findViewById(R.id.left);
right = (CheckBox) findViewById(R.id.right);
up.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(RadioMain.this, "上", Toast.LENGTH_SHORT)
.show();
}
}
});
down.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
Toast.makeText(RadioMain.this, "下", Toast.LENGTH_SHORT).show();
}
}
});
left.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(RadioMain.this, "左", Toast.LENGTH_SHORT).show();
}
}
});
right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
Toast.makeText(RadioMain.this, "右", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_radio_main, menu);
return true;
}
}
最终结果
文章来源: blog.csdn.net,作者:fengda2870,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/fengda2870/article/details/8155412
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)