如何使用Java Android选择单选按钮值?

时间:2016-05-03 05:08:19

标签: java android eclipse radio-button

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

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")

Pick Services

2 个答案:

答案 0 :(得分:0)

您可以使用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");
}

答案 1 :(得分:0)

这是在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();
    }

结果

enter image description here