Android设置RadioButton在文字的右边

本文介绍了如何在Android新手和高手之间做出选择,通过代码示例展示了如何使用RadioGroup实现单选按钮组,并通过事件监听调整TextView显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/radiobutton_textview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="50dip"  
  11.         android:background="@android:drawable/title_bar"  
  12.         android:gravity="center_vertical"  
  13.         android:textAppearance="?android:attr/textAppearanceLarge"  
  14.         android:textSize="18dip"  
  15.         android:textStyle="bold" />  
  16.   
  17.     <RadioGroup  
  18.         android:id="@+id/group"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:orientation="vertical" >  
  22.   
  23.         <RadioButton  
  24.             android:id="@+id/button1"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="50dip"  
  27.             android:button="@null"  
  28.             android:drawableRight="@android:drawable/btn_radio"  
  29.             android:paddingLeft="30dip"  
  30.             android:text="Android新手"  
  31.             android:textSize="20dip" />  
  32.   
  33.         <View  
  34.             android:layout_width="fill_parent"  
  35.             android:layout_height="1px"  
  36.             android:background="?android:attr/listDivider" />  
  37.   
  38.         <RadioButton  
  39.             android:id="@+id/button2"  
  40.             android:layout_width="fill_parent"  
  41.             android:layout_height="50dip"  
  42.             android:button="@null"  
  43.             android:drawableRight="@android:drawable/btn_radio"  
  44.             android:paddingLeft="30dip"  
  45.             android:text="Android高手"  
  46.             android:textSize="20dip" />  
  47.     </RadioGroup>  
  48.       
  49. </LinearLayout>  
[java]  view plain copy
  1. package sdfasdf.sadf;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.RadioGroup;  
  6. import android.widget.RadioGroup.OnCheckedChangeListener;  
  7. import android.widget.TextView;  
  8.   
  9. public class SdfsadfasdfasdffActivity extends Activity {  
  10.     private TextView textView;    
  11.     private RadioGroup group;    
  12.       
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         textView = (TextView) findViewById(R.id.radiobutton_textview);    
  18.         group = (RadioGroup) findViewById(R.id.group);    
  19.           
  20.         // 单选按钮组监听事件    
  21.         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {    
  22.     
  23.             @Override    
  24.                 public void onCheckedChanged(RadioGroup group, int checkedId) {    
  25.                 // 根据ID判断选择的按钮    
  26.                     if (checkedId == R.id.button1) {    
  27.                         textView.setText("Android新手");    
  28.                     } else {    
  29.                             textView.setText("Android高手");    
  30.                     }    
  31.                 }    
  32.           });  
  33.     }  
  34. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值