简体   繁体   English

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

[英]How to Choose Radio Button Value using Java Android?

If i'm click OK how to choose pick a value one service in radio button but different value. 如果我单击确定,如何选择单选按钮中的一项服务,但选择另一项值。

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

In this line for primary service : 在这行主要服务中:

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

How i make conditional if service other give value : 如果服务其他能给我带来价值,我该如何使其成为条件:

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

接送服务

You can get the selected item id of RadioGroup using, radioGroup.getCheckedRadioButtonId() and use if-else to choose different strings. 您可以使用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");
}

This is code snippet how you can achieve this with the help of RadioGroups 这是代码段,您如何在RadioGroups的帮助下实现此目标

Your dialog layout should look like this :- 您的对话框布局应如下所示:-

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

And you can inflate this layout in custom dialog box and with the help of radio buttons you can choose service type 您可以在自定义对话框中为这种布局充气,并借助单选按钮选择服务类型

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

Result 结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM