android RadioGroup的使用方法
更新時(shí)間:2012年11月09日 09:51:31 作者:
android RadioGroup的使用方法,需要的朋友可以參考下
創(chuàng)建一個(gè)MainActivity.java的主類
<?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">
<TextView
android:id="@+id/radiobutton_textview"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textSize="18dip"
android:textStyle="bold"
android:background="@android:drawable/title_bar"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
<RadioGroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textSize="20dip"
android:paddingLeft="30dip"
android:text="Android新手"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"/>
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="?android:attr/listDivider"
/>
<RadioButton
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textSize="20dip"
android:paddingLeft="30dip"
android:text="Android高手"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"/>
</RadioGroup>
</LinearLayout>
Xml代碼
package endual.radio;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private RadioGroup group;
private RadioButton rb1 ;
private RadioButton rb2 ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.radiobutton_textview);
group = (RadioGroup) findViewById(R.id.group);
this.rb1 = (RadioButton) this.findViewById(R.id.button1) ;
this.rb2 = (RadioButton) this.findViewById(R.id.button2) ;
// 單選按鈕組監(jiān)聽事件
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 根據(jù)ID判斷選擇的按鈕
if (checkedId == R.id.button1) {
textView.setText("Android新手");
rb1.setText("我是1") ;
String msg = rb1.getText().toString() ; //獲取單獨(dú)的radioButton的按鈕
rb2.setText(msg) ;
//System.out.println();
} else {
textView.setText("Android高手");
rb2.setText("我是2") ;
}
}
});
}
}
RadioGroup注冊(cè)監(jiān)聽事件OnCheckedChangeListener(),在onCheckedChanged實(shí)現(xiàn)業(yè)務(wù)邏輯。
復(fù)制代碼 代碼如下:
<?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">
<TextView
android:id="@+id/radiobutton_textview"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textSize="18dip"
android:textStyle="bold"
android:background="@android:drawable/title_bar"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
<RadioGroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textSize="20dip"
android:paddingLeft="30dip"
android:text="Android新手"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"/>
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="?android:attr/listDivider"
/>
<RadioButton
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textSize="20dip"
android:paddingLeft="30dip"
android:text="Android高手"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"/>
</RadioGroup>
</LinearLayout>
Xml代碼
復(fù)制代碼 代碼如下:
package endual.radio;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private RadioGroup group;
private RadioButton rb1 ;
private RadioButton rb2 ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.radiobutton_textview);
group = (RadioGroup) findViewById(R.id.group);
this.rb1 = (RadioButton) this.findViewById(R.id.button1) ;
this.rb2 = (RadioButton) this.findViewById(R.id.button2) ;
// 單選按鈕組監(jiān)聽事件
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 根據(jù)ID判斷選擇的按鈕
if (checkedId == R.id.button1) {
textView.setText("Android新手");
rb1.setText("我是1") ;
String msg = rb1.getText().toString() ; //獲取單獨(dú)的radioButton的按鈕
rb2.setText(msg) ;
//System.out.println();
} else {
textView.setText("Android高手");
rb2.setText("我是2") ;
}
}
});
}
}
RadioGroup注冊(cè)監(jiān)聽事件OnCheckedChangeListener(),在onCheckedChanged實(shí)現(xiàn)業(yè)務(wù)邏輯。
您可能感興趣的文章:
- Android控件系列之RadioButton與RadioGroup使用方法
- android自定義RadioGroup可以添加多種布局的實(shí)現(xiàn)方法
- Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
- Android開發(fā)之RadioGroup的簡(jiǎn)單使用與監(jiān)聽示例
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android RadioGroup 設(shè)置某一個(gè)選中或者不可選中的方法
- Android使用RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄
- Android實(shí)現(xiàn)界面內(nèi)嵌多種卡片視圖(ViewPager、RadioGroup)
- Android 自定義View實(shí)現(xiàn)任意布局的RadioGroup效果
- Android基礎(chǔ)控件RadioGroup使用方法詳解
相關(guān)文章
Android ListView實(shí)現(xiàn)仿iPhone實(shí)現(xiàn)左滑刪除按鈕的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狝ndroid ListView實(shí)現(xiàn)仿iPhone實(shí)現(xiàn)左滑刪除按鈕的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08Android 使用【AIDL】調(diào)用外部服務(wù)的解決方法
本篇文章是對(duì)Android中使用AIDL調(diào)用外部服務(wù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06android自定義popupwindow仿微信右上角彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了android自定義popupwindow仿微信右上角彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android 搜索結(jié)果匹配關(guān)鍵字且高亮顯示功能
這篇文章主要介紹了Android 搜索結(jié)果匹配關(guān)鍵字且高亮顯示功能,需要的朋友可以參考下2017-05-05解析android res 運(yùn)行錯(cuò)誤的問題
本篇文章是對(duì)android中res運(yùn)行錯(cuò)誤的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06