當前位置:首頁 » 安卓系統 » android的radiobutton

android的radiobutton

發布時間: 2023-02-01 00:27:40

1. android radiobutton怎麼用

android RadioButton單選框的使用方法:

public class MainActivity extends Activity {

public RadioGroup mRadioGroup1;
public RadioButton mRadio1, mRadio2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRadioGroup1 = (RadioGroup) findViewById(R.id.gendergroup);
mRadio1 = (RadioButton) findViewById(R.id.girl);
mRadio2 = (RadioButton) findViewById(R.id.boy);
mRadioGroup1.setOnCheckedChangeListener(radiogpchange);
}
private RadioGroup.OnCheckedChangeListener radiogpchange = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == mRadio1.getId()) {
Toast.makeText(getApplicationContext(), "女孩", 1).show();
} else if (checkedId == mRadio2.getId()) {
Toast.makeText(getApplicationContext(), "男孩", 1).show();
}
}
};
}

2. Android-RadioButton單選按鈕控制項詳解

RadioButton是單選按鈕,允許用戶在一個組中選擇一個選項。同一組中的單選按鈕有互斥效果。

1.RadioButton是圓形單選框;
2.RadioGroup是個可以容納多個RadioButton的容器;
3.在RadioGroup中的RadioButton控制項可以有多個,但同時有且僅有一個可以被選中。

基本的使用就是上面Demo說的那樣。在Android開發當中,我們也可以使用RadioButton控制項來實現應用的底部導航欄。

3. android radiobutton點擊後一直是選中狀態,怎麼再點擊後取消選中

設置一個全局變數為RadioButton的狀態,設置RadioButton點擊監聽事件,監聽你是否點擊按鈕,如果按鈕是點擊狀態,那再次點擊後就會取消選中。

代碼如下:
final RadioButton rb_bug = (RadioButton) view.findViewById(R.id.rb_buy);
final GlobalValue globalValue = new GlobalValue();
rb_bug.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isCheck = globalValue.isCheck();
if(isCheck)
{
if(v==rb_bug)rb_bug.setChecked(false);
}
else
{
if(v==rb_bug)rb_bug.setChecked(true);
}
globalValue.setCheck(!isCheck);
}
});
public class GlobalValue {
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
private boolean isCheck;
}

(3)android的radiobutton擴展閱讀:

RadioButton使用步驟

1、RadioButton是圓形單選框

2、RadioGroup是個可以容納多個RadioButton的容器。

3、在RadioGroup中的RadioButton控制項可以有多個,但同時有且僅有一個可以被選中。

代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="請選擇性別" />

<RadioGroup

android:id="@+id/rg_sex"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<RadioButton

android:id="@+id/rb_Male"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="男" />

<RadioButton

android:id="@+id/rb_FeMale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="女" />

</RadioGroup>

</LinearLayout>

網路:RadioButton

4. android radiobutton點擊後一直是選中狀態,怎麼再點擊後取消選中

Radiobutton既單選框,多個單選框中必須有一個是選中的,如果你想選擇之後又取消,那麼要使用CheckBox來實現改變它的狀態,方式有三種:

1、XML中申明 android:check="true|false"

2、代碼動態改變 checkBox.setChecked(true|false)

3、用戶觸摸 這個由android系統自動改變

RadioButton使用步驟:

1、RadioButton是圓形單選框

2、RadioGroup是個可以容納多個RadioButton的容器。

3、在RadioGroup中的RadioButton控制項可以有多個,但同時有且僅有一個可以被選中。

代碼如下:

final RadioButton rb_bug = (RadioButton) view.findViewById(R.id.rb_buy);

final GlobalValue globalValue = new GlobalValue();

rb_bug.setOnClickListener(new View.OnClickListener()

{@Overridepublic void onClick(View v) {boolean isCheck = globalValue.isCheck();

if(isCheck{if(v==rb_bug)rb_bug.setChecked(false);}

else{if(v==rb_bug)rb_bug.setChecked(true);}globalValue.setCheck(!isCheck);}});

public class GlobalValue {public boolean isCheck() {return isCheck;}public void setCheck(boolean check)

{isCheck = check;}private boolean isCheck;}

5. android RadioButton怎麼設置默認選中

android RadioButto這個控制項是不能設置默認選中的,因為這個是谷歌公司源碼中規定的要想設置為選中狀態,需要手動添加一個屬性,屬性為checked,把該屬性設置為true,設置方法如下:

1、使用Android studio創建一個項目,如下圖:

6. 求 android RadioButton屬性詳解

一: 單選按鈕RadioButton在Android平台上也應用的非常多,比如一些選擇項的時候,會用到單選按鈕,實現單選按鈕由兩部分組成,也就是RadioButton和RadioGroup配合使用

RadioButton的單選按鈕;

RadioGroup是單選組合框,用於將RadioButton框起來;

在沒有RadioGroup的情況下,RadioButton可以全部都選中;

當多個RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個;

注意:單選按鈕的事件監聽用setOnCheckedChangeListener來對單選按鈕進行監聽

例子:一道選擇題,選擇哪個城市美女最多,當然,這個就是為了測試

Java代碼
1.package org.loulijun.radio;
2.
3.import android.app.Activity;
4.import android.os.Bundle;
5.import android.view.Gravity;
6.import android.widget.RadioButton;
7.import android.widget.RadioGroup;
8.import android.widget.TextView;
9.import android.widget.Toast;
10.
11.public class RadioTest extends Activity {
12. /** Called when the activity is first created. */
13. TextView textview;
14. RadioGroup radiogroup;
15. RadioButton radio1,radio2,radio3,radio4;
16. @Override
17. public void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.main);
20. textview=(TextView)findViewById(R.id.textview1);
21. radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);
22. radio1=(RadioButton)findViewById(R.id.radiobutton1);
23. radio2=(RadioButton)findViewById(R.id.radiobutton2);
24. radio3=(RadioButton)findViewById(R.id.radiobutton3);
25. radio4=(RadioButton)findViewById(R.id.radiobutton4);
26.
27. radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
28.
29. @Override
30. public void onCheckedChanged(RadioGroup group, int checkedId) {
31. // TODO Auto-generated method stub
32. if(checkedId==radio2.getId())
33. {
34. DisplayToast("正確答案:"+radio2.getText()+",恭喜你,回答正確!");
35. }else
36. {
37. DisplayToast("請注意,回答錯誤!");
38. }
39. }
40. });
41. }
42. public void DisplayToast(String str)
43. {
44. Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
45. toast.setGravity(Gravity.TOP,0,220);
46. toast.show();
47. }
48.}

strings.xml文件

Xml代碼
1.<?xml version="1.0" encoding="utf-8"?>
2.<resources>
3. <string name="hello">哪個城市美女多?</string>
4. <string name="app_name">單選按鈕測試</string>
5. <string name="radiobutton1">杭州</string>
6. <string name="radiobutton2">成都</string>
7. <string name="radiobutton3">重慶</string>
8. <string name="radiobutton4">蘇州</string>
9.</resources>

main.xml文件:注意,這裡面,4個RadioButton包含在RadioGroup中

Xml代碼
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7.<TextView
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. android:text="@string/hello"
11. android:id="@+id/textview1"
12. />
13. <RadioGroup
14. android:id="@+id/radiogroup1"
15. android:layout_width="wrap_content"
16. android:layout_height="wrap_content"
17. android:orientation="vertical"
18. android:layout_x="3px"
19. >
20. <RadioButton
21. android:id="@+id/radiobutton1"
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:text="@string/radiobutton1"
25. />
26. <RadioButton
27. android:id="@+id/radiobutton2"
28. android:layout_width="wrap_content"
29. android:layout_height="wrap_content"
30. android:text="@string/radiobutton2"
31. />
32. <RadioButton
33. android:id="@+id/radiobutton3"
34. android:layout_width="wrap_content"
35. android:layout_height="wrap_content"
36. android:text="@string/radiobutton3"
37. />
38. <RadioButton
39. android:id="@+id/radiobutton4"
40. android:layout_width="wrap_content"
41. android:layout_height="wrap_content"
42. android:text="@string/radiobutton4"
43. />
44. </RadioGroup>
45.</LinearLayout>

二:Android 自定義RadioButton的樣式(和上面關系不大)

我們知道Android控制項里的button,listview可以用xml的樣式自定義成自己希望的漂亮樣式。

最近用到RadioButton,利用xml修改android:background="@drawable/button_drawable",其中button_drawable為自己定義的.xml文件(res/drawable文件下),但是不成功,到網上查找,也沒有正確的說法,我就開始自己嘗試,最後做好了。

其實方法很簡單,同樣在res/drawable新建radiobutton.xml如下

Xml代碼
1.<selector xmlns:android="http://schemas.android.com/apk/res/android">
2.
3. <item
4.
5.
6. android:state_enabled="true"
7.
8. android:state_checked="true"
9.
10. android:drawable="@drawable/check" />
11.
12. <item
13.
14. android:state_enabled="true"
15.
16. android:state_checked="false"
17.
18. android:drawable="@drawable/checknull" />
19.
20. </selector>
check和checknull分別為選中和位選中的圖片。
然後在你的布局文件中,RadioButton 布局
設置android:button = "@drawable/radiobutton",就可以了!

熱點內容
linuxshellif 發布:2024-05-04 17:09:47 瀏覽:16
演算法精英挑戰賽 發布:2024-05-04 17:09:08 瀏覽:739
河南電力公眾號綁定密碼是多少 發布:2024-05-04 17:08:55 瀏覽:331
手機上怎麼打開壓縮文件 發布:2024-05-04 17:03:57 瀏覽:172
word加密文件如何解密 發布:2024-05-04 17:02:57 瀏覽:289
php源碼本地測試 發布:2024-05-04 16:57:17 瀏覽:801
c語言編譯exe 發布:2024-05-04 16:57:16 瀏覽:974
國密演算法獲取 發布:2024-05-04 16:38:24 瀏覽:70
腳本精靈荒野亂斗 發布:2024-05-04 16:28:33 瀏覽:520
剛到的筆記本怎麼看配置 發布:2024-05-04 16:26:58 瀏覽:4