【问题标题】:How to Choose Radio Button Value using Java Android?如何使用 Java Android 选择单选按钮值?
【发布时间】:2016-08-28 00:27:18
【问题描述】:

如果我单击“确定”,如何在单选按钮中选择一个服务但不同的值。

void OpenDialogService() {

    closeLyt.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    cancelTxt.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    okTxt.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            Bundle b    =   activity.getIntent().getExtras();
            if(b == null)
                b = new Bundle();
            b.putString("service_id", "1");
            startActivity(new Intent(activity, Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
            overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
        }
    });

    dialog.show();
}

在这行主要服务:

b.putString("service_id", "1")

如果服务其他给予价值,我如何设定条件:

b.putString("service_id", "2")

【问题讨论】:

  • 问题不清楚,请详细说明您要达到的目标。
  • 对不起,我是新手,我尝试询问如何在单选按钮中获取值,在我的情况下,我在单选按钮中只得到一个值,我给出了 screnshoot。
  • 现在问题很清楚了@HiteshSahu?
  • @SeptianMaulana 请编辑问题并在此处插入说明。
  • 好的,下次我将插入澄清问题,,,:)

标签: java android eclipse radio-button


【解决方案1】:

这是代码 sn-p 如何在 RadioGroups 的帮助下实现这一目标

您的对话框布局应如下所示:-

<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"
    android:orientation="vertical"
    tools:context="com.example.test.MainActivity" >

    <TextView
        android:id="@+id/headerTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="#ff669900"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:text="Pick Service"
        android:textColor="#FFF"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_margin="10dp"
        android:layout_marginTop="32dp" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/primaryTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:checked="true"
                android:text="Primary Service"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/primaryContentTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/primaryTxt"
                android:text=" -Dusting \n -Sweeping \n -Washing \n -Cleaning " />

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" >

                <RadioButton
                    android:id="@+id/service_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     />

                <RadioButton
                    android:id="@+id/service_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="50dp" />
            </RadioGroup>

            <TextView
                android:id="@+id/otherTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/primaryContentTxt"
                android:layout_marginTop="10dp"
                android:checked="true"
                android:gravity="left"
                android:text="Other Service(Soon)"
                android:textSize="18sp" />
        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/closeLyt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:weightSum="2" >

        <TextView
            android:id="@+id/cancelTxt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#fff"
            android:gravity="center"
            android:padding="5dp"
            android:text="CANCEL"
            android:textColor="#000"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/okTxt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#fff"
            android:gravity="center"
            android:padding="5dp"
            android:text="OK"
            android:textColor="#000"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

您可以在自定义对话框中扩展此布局,并借助单选按钮选择服务类型

int serviceNumber = 0;

    void OpenDialogService() {

        final Dialog dialog = new Dialog(this);
        dialog.getWindow();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.pop_new_order2);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        TextView headerTxt = (TextView) dialog.findViewById(R.id.headerTxt);
        TextView okTxt = (TextView) dialog.findViewById(R.id.okTxt);
        TextView cancelTxt = (TextView) dialog.findViewById(R.id.cancelTxt);
        TextView primaryTxt = (TextView) dialog.findViewById(R.id.primaryTxt);
        TextView otherTxt = (TextView) dialog.findViewById(R.id.otherTxt);
        TextView primaryContentTxt = (TextView) dialog.findViewById(R.id.primaryContentTxt);
        LinearLayout closeLyt = (LinearLayout) dialog.findViewById(R.id.closeLyt);

        // Radio Buttons

        final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
        radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                  View radioButton = radio.findViewById(checkedId);
                    int index = radio.indexOfChild(radioButton);

                    Toast.makeText(getApplicationContext(), "service" +index, 500).show();

                    serviceNumber = index+1;

            }
        });

        headerTxt.setText("Pick Services");

        //TODO change color here
        // headerTxt.setBackgroundResource(R.drawable.bg_dialog_header_success);
        // closeLyt.setBackgroundResource(R.color.selector_close_alert_dialog_success);

        closeLyt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        cancelTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        okTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Bundle b = getIntent().getExtras();
                if (b == null)
                    b = new Bundle();
                b.putString("service_id", String.valueOf(serviceNumber));

                Toast.makeText(getApplicationContext(), "service " + String.valueOf(serviceNumber), 500).show();

                //TODO Handle Activity Transition Here
                // startActivity(new Intent(MainActivity.this,
                // Step1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtras(b));
                // overridePendingTransition(R.anim.push_left_in,
                // R.anim.push_left_out);
            }
        });

        dialog.show();
    }

结果

【讨论】:

  • 感谢 Hitesh Sahu 我已经尝试过了我想要检查按钮,,,但总是错误,,,
  • 因为可能 RadioGroup 没有被清除,你有没有参考 RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);如果可能,分享您的活动类和堆栈跟踪
  • 哦,我的朋友,非常感谢,,,,,我只是没有把“对话”放在最后的 RadioGroup radioGroups = (RadioGroup) findViewById(R.id.radioService); ,,,,,最终代码是final RadioGroup radioGroups = (RadioGroup) dialog.findViewById(R.id.radioService); ,,,,, 多谢! :)...我非常沮丧,专注于条件总是失败,,并且错误未放入对话框,,,:v,,,给我经验,,,谢谢@hitesh Sahu,,,
  • 并且每个代码都是有条件的,在我第一次写时总是正确的,,,问题只是没有放“对话”,,,你救了我的一天...... ^-^
  • 是的,我现在可以舒服地回家了你可以帮助我,:)
【解决方案2】:

您可以使用radioGroup.getCheckedRadioButtonId() 获取 RadioGroup 的选定项 id,并使用 if-else 选择不同的字符串。

int id = radioGroup.getCheckedRadioButtonId();
if(id == radioButton1)
{
    b.putString("service_id", "1");
}
else if(id == radioButton1)
{
    b.putString("service_id", "2");
}

【讨论】:

  • 是的,我的朋友我现在已经使用单选组了,在我只使用单选按钮之前还是 2 按钮选择,看这张图片prnt.sc/azoqa8 谢谢...但是我有一些问题,,,java .lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.widget.RadioGroup.getCheckedRadioButtonId()”。
  • 请看这个,我对那个错误很困惑pastebin.com/BDdimhUY.. >/
  • 您应该将单选组引用分配给变量。 RadioGroup radiogroup = findViewById(R.id.your_radio_group_ID);
  • 是的 bob 已经是,我的 id 单选组是 radioService...但是运行时的错误,,,在我执行 OK 按钮...不幸的是已经停止了..
  • 您的 RadioGroup 的 id 是 radioService。您应该从 java 类中获取该 RadioGroup 的引用。 RadioGroup radiogroup = (RadioGroup)findViewById(R.id.your_radio_group_ID);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
相关资源
最近更新 更多