第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Android從選定的單選按鈕獲取價(jià)值

Android從選定的單選按鈕獲取價(jià)值

躍然一笑 2019-10-28 14:19:01
我有一段代碼,其中RadioButton一個(gè)包含三個(gè)s RadioGroup。我想設(shè)置的onCheckedListener,將顯示的值RadioButton的Toast。但是,到目前為止我沒有得到任何結(jié)果。如何獲取的值RadioButton并將其顯示在中Toast?預(yù)先謝謝您,這是我的代碼:import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;public class MainActivity extends Activity {    RadioGroup rg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);        final String value =            ((RadioButton)findViewById(rg.getCheckedRadioButtonId()))            .getText().toString();        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {            public void onCheckedChanged(RadioGroup group, int checkedId) {                Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();            }        });    }}XML檔案:<RelativeLayout     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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <RadioGroup        android:id="@+id/radioGroup1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="152dp" > </RelativeLayout>
查看完整描述

3 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊

對(duì)于以編程方式填充并希望獲取索引的任何人,您可能會(huì)注意到,當(dāng)您返回到活動(dòng)/片段并重新添加這些單選按鈕時(shí),checkedId會(huì)更改。解決該問題的一種方法是使用索引設(shè)置標(biāo)簽:


    for(int i = 0; i < myNames.length; i++) {

        rB = new RadioButton(getContext());

        rB.setText(myNames[i]);

        rB.setTag(i);

        myRadioGroup.addView(rB,i);

    }

然后在您的聽眾中:


    myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override

        public void onCheckedChanged(RadioGroup group, int checkedId) {

            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);

            int mySelectedIndex = (int) radioButton.getTag();

        }

    });


查看完整回答
反對(duì) 回復(fù) 2019-10-28
  • 3 回答
  • 0 關(guān)注
  • 822 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)