android toast 和checkbox and radiogroup的使用
16lz
2021-01-23
1. main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/groupId" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" android:checked="true" /> <RadioButton android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> </RadioGroup> <TextView android:id="@+id/love" android:text="@string/love" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/swinning" android:text="@string/swinning" android:layout_width="wrap_content" android:layout_height="wrap_content"> </CheckBox> <CheckBox android:id="@+id/running" android:text="@string/running" android:layout_width="wrap_content" android:layout_height="wrap_content"> </CheckBox> <CheckBox android:id="@+id/reading" android:text="@string/reading" android:layout_width="wrap_content" android:layout_height="wrap_content"> </CheckBox></LinearLayout>
2. Java 调用
package com.helloworld;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;/** * 常控件的使用 * @author liuqing * @version 1.0 * */public class ViewCompoentActivity extends Activity {private RadioGroup radioGroup;private CheckBox running;private CheckBox reading;private CheckBox swinning; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.radioGroup = (RadioGroup)this.findViewById(R.id.groupId); this.reading = (CheckBox)this.findViewById(R.id.reading); this.swinning = (CheckBox)this.findViewById(R.id.swinning); this.running = (CheckBox)this.findViewById(R.id.running); //对readioGroup 添加监听器 this.radioGroup.setOnCheckedChangeListener( new OnCheckedChangeListener(){ //这里是返加的ID值@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId == R.id.female) {Toast.makeText(getApplication(), "female",Toast.LENGTH_SHORT).show();}else if (checkedId == R.id.male) {Toast.makeText(getApplication(), "male", Toast.LENGTH_SHORT).show();}} } ); this.running.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {//如果被选中isChecked 值为trueif (isChecked) {System.out.println("running is true");Toast.makeText(getApplication(), "running is true", Toast.LENGTH_SHORT).show();}else {System.out.println("running is false");}} } ); this.swinning.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {System.out.println("swinning is true");Toast.makeText(getApplication(), "swinning is true", Toast.LENGTH_SHORT).show();}else {System.out.println("swinning is false");}} } ); this.reading.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {System.out.println("reading is true");Toast.makeText(getApplication(), "reading is true",Toast.LENGTH_SHORT).show();}else {System.out.println("reading is false");}} } ); }}
更多相关文章
- Android控件笔记——在界面中显示图片
- Android 获取控件宽高的3种方法
- Android UI控件详解-CheckBox(多选框)
- Android UI控件详解-Gallery(画廊)
- Android UI控件详解-ImageSwitcher(图片切换器)
- Android 滑动开关控件
- Android 学习之- 单选按钮、复选框、状态开关、时钟控件
- Android 控件之八:AutoCompleteTextView自动完成文本框
- Android UI控件详解-RadioGroup和RadioButton(单选框)