當前位置:首頁 » 安卓系統 » androidradiogroup

androidradiogroup

發布時間: 2022-06-22 13:52:05

① Android如何動態生成Radio和RadioGroup

privateLinearLayoutlayout;//布局,可以在xml布局中獲得

privateRadioGroupgroup;//點選按鈕組

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

layout=newLinearLayout(this);//實例化布局對象

group=newRadioGroup(this);//實例化單選按鈕組

//添加單選按鈕

for(inti=0;i<5;i++){

RadioButtonradio=newRadioButton(this);

radio.setText("radio"+i);

group.addView(radio);

}

//將單選按鈕組添加到布局中

layout.addView(group);

this.setContentView(layout);

}

可以把單選按鈕組放在ScrollView中,這樣的話,多出的部分可以滾動查看了。

② android radiogroup怎麼用

RadioButton和RadioGroup的關系:1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器2、每個RadioGroup中的RadioButton同時只能有一個被選中3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中4、大部分場合下,一個RadioGroup中至少有2個RadioButton5、大部分場合下,一個RadioGroup中的RadioButton默認會有一個被選中,並建議您將它放在RadioGroup中的起始位置。

③ android的radiogroup怎麼移動布局

實際上只要我們明白在radiogroup裡面我們也可以使用RelativeLayout,LinearLayout這樣的布局的;首先設置radiogroup的orientation屬性為vertical

然後再第一個radiobutton前面加上LinearLayout,orientation屬性設置為horizontal,</LinearLayout>標簽放在一行最後一個radiobutton後面;小編這里是 文本為「50」的那個radiobutton後面

同樣的把使用LinearLayout把後面幾個radiobutton包裹住,orientation屬性設置為horizontal,

運行一下就可以發現就達到了我們想要的結果!

④ android 中如何獲取radiogroup 中那個radiobutton被選擇

java">radiogroup本身有監聽的方法可以直接設置監聽,這個監聽需要一個回調介面OnCheckedChangeListener,這個介面裡面的回調方法給我們返回了兩個參數其中int型的參數就是當前你選中的RadioButton的ID
radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
//checkId就是當前選中的RadioButton
}
});

⑤ android里RadioGroup的clearCheck()使用方法

實現RadioButton由兩部分組成,也就是RadioButton和RadioGroup配合使用.RadioGroup是單選組合框,可以容納多個RadioButton的容器.在沒有RadioGroup的情況下,RadioButton可以全部都選中;當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個。並用setOnCheckedChangeListener來對單選按鈕進行監聽

01 RadioGroup相關屬性:
02
03 RadioGroup.getCheckedRadioButtonId ();--獲取選中按鈕的id
04
05 RadioGroup.clearCheck ();//---清除選中狀態
06
07 RadioGroup.check (int id);//---通過參入選項id來設置該選項為選中狀態如果傳遞-1作為指定的選擇標識符來清除單選按鈕組的勾選狀態,相當於調用clearCheck()操作
08
09 setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener); //--一個當該單選按鈕組中的單選按鈕勾選狀態發生改變時所要調用的回調函數
10
11 addView (View child, int index, ViewGroup.LayoutParams params);//---使用指定的布局參數添加一個子視圖
12
13 //參數 child 所要添加的子視圖 index 將要添加子視圖的位置 params 所要添加的子視圖的布局參數
14
15 RadioButton.getText();//獲取單選框的值
16
17 //此外,RadioButton的checked屬性設置為true,代碼里調用RadioButton的check(id)方法,不會觸發onCheckedChanged事件

RadioButton和RadioGroup的關系:
1、RadioButton表示單個圓形單選框,而RadioGroup是可以容納多個RadioButton的容器
2、每個RadioGroup中的RadioButton同時只能有一個被選中
3、不同的RadioGroup中的RadioButton互不相干,即如果組A中有一個選中了,組B中依然可以有一個被選中
4、大部分場合下,一個RadioGroup中至少有2個RadioButton
5、大部分場合下,一個RadioGroup中的RadioButton默認會有一個被選中,並建議您將它放在RadioGroup中的起始位置

看案例:
1.定義布局文件:

01 <?xml version="1.0" encoding="utf-8"?>
02 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="match_parent"
04 android:layout_height="match_parent" >
05 <LinearLayout
06 android:orientation="vertical"
07 android:layout_width="match_parent"
08 android:layout_height="wrap_content"
09 android:layout_marginRight="5dp" >
10
11 <TextView
12 android:id="@+id/radiogroup_info_id"
13 android:layout_width="228px"
14 android:layout_height="wrap_content"
15 android:text="我選擇的是...?"
16 android:textSize="30sp"
17 />
18
19 <RadioGroup
20 android:id="@+id/radioGroup_sex_id"
21 android:layout_width="match_parent"
22 android:layout_height="match_parent"
23 >
24 <RadioButton
25 android:id="@+id/boy_id"
26 android:layout_width="match_parent"
27 android:layout_height="match_parent"
28 android:text="Boy"
29 />
30 <RadioButton
31 android:id="@+id/girl_id"
32 android:layout_width="match_parent"
33 android:layout_height="match_parent"
34 android:text="Girl"
35 />
36
37 </RadioGroup>
38 <Button
39 android:id="@+id/radio_clear"
40 android:layout_width="match_parent"
41 android:layout_height="match_parent"
42 android:text="清除選中按鈕"
43 />
44
45 <Button
46 android:id="@+id/radio_add_child"
47 android:layout_width="match_parent"
48 android:layout_height="match_parent"
49 android:text="添加單選項"
50 />
51
52 </LinearLayout>
53 </ScrollView>

2.java代碼文件

01 package com.dream.app.start.first.radiobutton;
02
03 import com.dream.app.start.R;
04 import com.dream.app.start.R.id;
05 import com.dream.app.start.R.layout;
06 import com.dream.app.start.three.utils.PublicClass;
07
08 import android.app.Activity;
09 import android.os.Bundle;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.view.ViewGroup.LayoutParams;
13 import android.widget.Button;
14 import android.widget.RadioButton;
15 import android.widget.RadioGroup;
16 import android.widget.RadioGroup.OnCheckedChangeListener;
17 import android.widget.TextView;
18 import android.widget.ToggleButton;
19
20 public class RadioButtonDemo extends PublicClass {
21 private TextView textView=null;
22 private RadioGroup radioGroup=null;
23 private RadioButton radioButton_boy,radioButton_girl;
24 private Button radio_clear,child;
25 /* (non-Javadoc)
26 * <a href="http://my.oschina.net/u/244147" target="_blank" rel="nofollow">@see</a> android.app.Activity#onCreate(android.os.Bundle)
27 */
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 // TODO Auto-generated method stub
31 super.onCreate(savedInstanceState);
32
33 setContentView(R.layout.layout_frist_radiobuton);
34
35 textView = (TextView)findViewById(R.id.radiogroup_info_id);
36
37 //radioGroup
38 radioGroup=(RadioGroup)findViewById(R.id.radioGroup_sex_id);
39 radioButton_boy=(RadioButton)findViewById(R.id.boy_id);
40 radioButton_girl=(RadioButton)findViewById(R.id.girl_id);
41 child=(Button)findViewById(R.id.radio_add_child);
42 //---
43 radioGroup.setOnCheckedChangeListener(listen);
44 radio_clear=(Button)findViewById(R.id.radio_clear);
45 radio_clear.setOnClickListener(onClick);
46 child.setOnClickListener(onClick);
47 }
48
49 private OnCheckedChangeListener listen=new OnCheckedChangeListener() {
50
51 @Override
52 public void onCheckedChanged(RadioGroup group, int checkedId) {
53 int id= group.getCheckedRadioButtonId();
54 switch (group.getCheckedRadioButtonId()) {
55 case R.id.girl_id:
56 textView.setText("我選擇的是:"+radioButton_girl.getText());
57 break;
58 case R.id.boy_id:
59 textView.setText("我選擇的是:"+radioButton_boy.getText());
60 break;
61 default:
62 textView.setText("我選擇的是:新增");
63 break;
64 }
65
66 }
67 };
68 private OnClickListener onClick=new OnClickListener() {
69
70 @Override
71 public void onClick(View v) {
72 radio_clear=(Button)v;
73 switch (radio_clear.getId()) {
74 case R.id.radio_clear:
75 radioGroup.check(-1);//清除選項
76 // radioGroup.clearCheck(); //清除選項
77 textView.setText("我選擇的是...?");
78 break;
79 case R.id.radio_add_child:
80 //新增子
81 RadioButton newRadio =new RadioButton(getApplicationContext());
82 newRadio.setText("新增");
83 radioGroup.addView(newRadio, radioGroup.getChildCount());
84 break;
85 //
86
87 default:
88 break;
89 }
90 }
91 };

⑥ android如何實現代碼控制RadioGroup中某一個按鈕選中

RadioButton在做表單的時候經常用到,在安卓開發中,RadioButton需要和RadioGroup一起使用,表示在一組可選項中,只有一
個可以被選中,RadioGroup狀態改變的一個監視器OnCheckedChangeListener,RadioGroup使用的時候調用
setOnCheckedChangeListener(),然後重寫OnCheckedChangeListener中的
onCheckedChanged()方法,比如:
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 獲取變更後的選項的ID
int radioButtonId = group.getCheckedRadioButtonId();
switch (radioButtonId) {
case R.id.message_radiobtn:
mFragment = new MessageFragment();
break;
case R.id.contact_radiobtn:
mFragment = new ContactFragment();
break;
case R.id.dynamic_radiobtn:
mFragment = new DynamicFragment();
break;
default:
break;
}
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.realtabcontent, mFragment).commit();
}
});這篇簡單寫了一個幾行代碼介紹,實現的效果有點類似QQ底部導航切換,Teachcourse博客:

⑦ android開發里的radiogroup單選 怎麼處理不選擇的情況

設置一個默認值,讓頁面一開始載入的時候就有一個選項,這樣就沒有不選擇的情況了。
mRbMonth.setChecked(true);
或在配置文件里加一個屬性:android:checked = "true"
都可以。

⑧ android開發radiogroup怎麼添加其他的控制項

radiogroup只能添加radiogbutton哈,是配套使用的控制項。

⑨ android的radiogroup為什麼選擇兩個

項目中遇到多個RadioGroup中單選RadioButton ,設置了默認選中第一個 . 然後就 能選中兩個RadioButton . . ..

我開始這樣給設置默認選中一個的:

for (int j = 0; j < newList.get(position).getList().size(); j++) {
RadioButton radioButton = new RadioButton(context);
radioButton.setTextSize(9);

radioButton.setText(newList.get(position).getList().get(j)
.get("dishname").toString());
radioButton.setTag(newList.get(position).getList().get(j)
.get("dishid").toString());
radioGroup.addView(radioButton, j);

if (j==0) {
radioButton.setCheck(true);
}
}

就是中給radioButton設置為選中.. .

網上查找了下類似的情況 如 這篇文章 ,都沒有解決我的問題.

最後研究了下 android 官方Api 和部分 RadioGroup的源代碼 後發現. 其實很簡單

我們不需要設置RadioButton的默認選中, 這樣會使RadioButton一直處於選中狀態.

我們應該給RadioGroup 設置選中的RadioButton ,也就是說

把 if (j==0) {
radioButton.setCheck(true);
}

更改為

if (j==0) {
radioGroup.check(radioButton.getId());
}

輕松搞定.. 哎呦了個去,官方Api和源碼是個好東西啊.

⑩ Android中的radiogroup中的radioButton都不可編輯

radiobutton radiobutton = new radiobutton(this);
radiogroup.layoutparams layoutparams = new radiogroup.layoutparams(radiogroup.layoutparams.wrap_content,50);
layoutparams.setmargins(10, 10, 10, 10);
radiobutton.setlayoutparams(layoutparams);
radiobutton.settext("rb1");
radiobutton.settextsize(12);
radiobutton.setgravity(gravity.center);
radiobutton.setpadding(10, 10, 10, 10);
radiogroup.addview(radiobutton);
//上面是添加1個,自己寫個循環添加吧。
//上面那個layoutparams必須是radiogroup的,因為radiobutton要添加的容器是radiogroup

熱點內容
sqlserver連接驅動 發布:2024-05-06 00:33:34 瀏覽:645
存儲開銷 發布:2024-05-06 00:13:38 瀏覽:953
伺服器怎麼盈利 發布:2024-05-05 23:56:16 瀏覽:941
java網站培訓學校 發布:2024-05-05 23:43:11 瀏覽:40
淘寶搜索演算法 發布:2024-05-05 23:37:07 瀏覽:998
sqlwhencasethen 發布:2024-05-05 23:27:51 瀏覽:641
模架編程軟體 發布:2024-05-05 23:26:54 瀏覽:483
存儲過程異常 發布:2024-05-05 23:24:03 瀏覽:399
winxp訪問不了win7 發布:2024-05-05 23:05:23 瀏覽:734
演算法牛 發布:2024-05-05 22:43:40 瀏覽:720