Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解
更新時間:2016年03月03日 11:21:49 投稿:mrr
在android程序開發(fā)中,無論是單選按鈕還是多選按鈕都非常的常見,接下來通過本文給大家介紹Android程序開發(fā)中單選按鈕(RadioGroup)的使用,需要的朋友參考下吧
在還沒給大家介紹單選按鈕(RadioGroup)的使用,先給大家展示下效果圖吧:
xml文件
<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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="您的性別為"/> <RadioGroup android:id="@+id/sex" android:layout_width="fill_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/male" android:text="男"/> <RadioButton android:id="@+id/female" android:text="女"/> </RadioGroup> </LinearLayout>
java文件
public class MainActivity extends Activity { private TextView txt=null; private RadioGroup sex=null; private RadioButton male=null; private RadioButton female=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.txt=(TextView) super.findViewById(R.id.txt); this.sex=(RadioGroup) super.findViewById(R.id.sex); this.male=(RadioButton) super.findViewById(R.id.male); this.female=(RadioButton) super.findViewById(R.id.female); this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImp()); } private class OnCheckedChangeListenerImp implements OnCheckedChangeListener{ public void onCheckedChanged(RadioGroup group, int checkedId) { String temp=null; if(MainActivity.this.male.getId()==checkedId){ temp="男"; } else if(MainActivity.this.female.getId()==checkedId){ temp="女"; } MainActivity.this.txt.setText("您的性別是"+temp); } }
以上所述是小編給大家介紹的Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解,希望對大家有所幫助!
相關(guān)文章
詳解Android Material Design自定義動畫的編寫
這篇文章主要介紹了詳解Android Material Design自定義動畫的編寫,其中對Activity的過渡動畫進行了重點講解,需要的朋友可以參考下2016-04-04Ubuntu16.04 LTS 下安裝 Android Studio 2.2.2 的詳細步驟
這篇文章主要介紹了Ubuntu16.04 LTS 下安裝 Android Studio 2.2.2 的詳細步驟,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11Android中DownloadManager實現(xiàn)文件下載實例詳解
這篇文章主要介紹了Android中DownloadManager實現(xiàn)文件下載實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03android ListView的右邊滾動滑塊啟用方法 分享
android ListView的右邊滾動滑塊啟用方法 分享,需要的朋友可以參考一下2013-05-05Android 中HttpURLConnection與HttpClient使用的簡單實例
這篇文章介紹了Android 中HttpURLConnection與HttpClient使用的簡單實例,有需要的朋友可以參考一下2013-10-10