你要當(dāng)成單選的一組的radiobutton要放在radioGROUP下,不然這個(gè)GROUP就沒(méi)起作用

創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元輝縣做網(wǎng)站,已為上家服務(wù),為輝縣各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
只有這樣寫(xiě),才能實(shí)現(xiàn)這組radiobutton是單選的效果。
1、使用checked屬性判斷選中,代碼為if($(this).attr("checked")){alert("選中了");}。
2、jquery獲取radio單選按鈕的值。以上就是androidlistview單選獲取勾選當(dāng)前選中指的方法。
一: 單選按鈕RadioButton在Android平臺(tái)上也應(yīng)用的非常多,比如一些選擇項(xiàng)的時(shí)候,會(huì)用到單選按鈕,實(shí)現(xiàn)單選按鈕由兩部分組成,也就是RadioButton和RadioGroup配合使用
RadioButton的單選按鈕;
RadioGroup是單選組合框,用于將RadioButton框起來(lái);
在沒(méi)有RadioGroup的情況下,RadioButton可以全部都選中;
當(dāng)多個(gè)RadioButton被RadioGroup包含的情況下,RadioButton只可以選擇一個(gè);
注意:?jiǎn)芜x按鈕的事件監(jiān)聽(tīng)用setOnCheckedChangeListener來(lái)對(duì)單選按鈕進(jìn)行監(jiān)聽(tīng)
例子:一道選擇題,選擇哪個(gè)城市美女最多,當(dāng)然,這個(gè)就是為了測(cè)試
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("請(qǐng)注意,回答錯(cuò)誤!");
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"哪個(gè)城市美女多?/string
4. string name="app_name"單選按鈕測(cè)試/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個(gè)RadioButton包含在RadioGroup中
Xml代碼
1.?xml version="1.0" encoding="utf-8"?
2.LinearLayout xmlns: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的樣式(和上面關(guān)系不大)
我們知道Android控件里的button,listview可以用xml的樣式自定義成自己希望的漂亮樣式。
最近用到RadioButton,利用xml修改android:background="@drawable/button_drawable",其中button_drawable為自己定義的.xml文件(res/drawable文件下),但是不成功,到網(wǎng)上查找,也沒(méi)有正確的說(shuō)法,我就開(kāi)始自己嘗試,最后做好了。
其實(shí)方法很簡(jiǎn)單,同樣在res/drawable新建radiobutton.xml如下
Xml代碼
1.selector xmlns: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 布局
設(shè)置android:button = "@drawable/radiobutton",就可以了!
文章標(biāo)題:android單選按鈕,android單選按鈕選中事件
分享路徑:http://chinadenli.net/article42/dsejshc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈、網(wǎng)站營(yíng)銷、網(wǎng)站策劃、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)