如何组合单选按钮和警报对话框

时间:2017-07-28 13:35:47

标签: java android radio-button alertdialog

所以我想做的是一个活动,它有4个选项,用单选按钮和下面的确认按钮创建,我试图做的是每次我选择其中一个4选项并点击"确认"按钮,出现一个警告对话框并询问客户"你选择x2,你确定吗? ",任何想法如何达到这个目标?

到目前为止我做了什么,但它只适用于第一个" x"。

public class MetalList extends AppCompatActivity {



RadioGroup radioGroup;
RadioButton radioButtonx;
RadioButton radioButtonx2;
RadioButton radioButtonx3;
RadioButton radioButtonx4;


TextView result;
Button btnSelect;
Button btnClear;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_metal_list);


    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

    radioButtonx = (RadioButton) findViewById(R.id.radioButton5);
    radioButtonx2 = (RadioButton) findViewById(R.id.radioButton2);
    radioButtonx3 = (RadioButton) findViewById(R.id.radioButton3);
    radioButtonx4 = (RadioButton) findViewById(R.id.x4Btn);

    result = (TextView) findViewById(R.id.metalListText);

    btnSelect = (Button) findViewById(R.id.btnSelectMetalList);
    btnClear = (Button) findViewById(R.id.btnclearMetalList);




    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {

            if (radioButtonx.isChecked()){

                txtResultMetalList.setText(" Gold selected - Press confirm to continue");
                txtResultMetalList.setTextColor(Color.parseColor("#ffd700"));


                String first = " selected ";
                String next = "<font color='#ffd700'>x </font>";
                result.setText(Html.fromHtml(next + first));


            }


            if (radioButtonx2.isChecked()){

                String first = " selected ";
                String next = "<font color='#c0c0c0'>x2 </font>";
                result.setText(Html.fromHtml(next + first));
            }

            if (radioButtonx3.isChecked()){

                String first = " selected ";
                String next = "<font color='#9b7d12'>x3 </font>";
                result.setText(Html.fromHtml(next + first));

            }

            if (radioButtonx4.isChecked()){

                String first = " selected";
                String next = "<font color='#6e2907'>x4 </font>";
                result.setText(Html.fromHtml(next + first));

            }
        }
    });






    Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {
        @Override
        public void run() {

           result.animate().alpha(1f).setDuration(1000);


        }
    },500);


    Handler handler2 = new Handler();
    handler2.postDelayed(new Runnable() {
        @Override
        public void run() {


            radioButtonx.animate().alpha(1f).setDuration(500);
            radioButtonx2.animate().alpha(1f).setDuration(1500);
            radioButtonx3.animate().alpha(1f).setDuration(2500);
            radioButtonx4.animate().alpha(1f).setDuration(3500);


        }
    },2000);

    Handler handler4 = new Handler();
    handler4.postDelayed(new Runnable() {
        @Override
        public void run() {

            radioButtonx.animate().translationX(-50f).setDuration(500);
            radioButtonx2.animate().translationX(-50f).setDuration(1500);
            radioButtonx3.animate().translationX(-50f).setDuration(2500);
            radioButtonx4.animate().translationX(-50f).setDuration(3500);




        }
    },2000);


    Handler handler3 = new Handler();
    handler3.postDelayed(new Runnable() {
        @Override
        public void run() {


            btnSelect.animate().alpha(1f).setDuration(1000);
            btnClear.animate().alpha(1f).setDuration(1000);

        }
    },2000);



   btnClear.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

           radioGroup.clearCheck();
           String next = "<font color='#ffffff'>Please select the desired metal</font>";
           result.setText(Html.fromHtml(next));


       }
   });

    btnSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int id = radioGroup.getCheckedRadioButtonId();
            btnSelect= (RadioButton) findViewById(id);








            if (radioButtonx.isChecked()){


            AlertDialog.Builder builder1 = new AlertDialog.Builder(MetalList.this);
            builder1.setTitle(" Request insurace ");
            builder1.setMessage(" You selected * x * , Are you sure? ");


            builder1.setPositiveButton(" Yes ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(getApplicationContext(), " x chosen - Transferring... ", Toast.LENGTH_SHORT).show();





                    Handler handlerNew = new Handler();
                    handlerNew.postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            Intent intentMove = new Intent(MetalList.this,LoadingScreen.class);
                            startActivity(intentMove);

                        }
                    },3000);






                }
            });

            builder1.setNegativeButton(" No ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    dialogInterface.cancel();
                    radioGroup.clearCheck();


                }
            });



            AlertDialog dialog = builder1.create();
            dialog.show();

        }



    }});




    if (radioButtonx2.isChecked()){


        AlertDialog.Builder builder2 = new AlertDialog.Builder(MetalList.this);
        builder2.setTitle(" Request insurance ");
        builder2.setMessage(" You selected * x2 * , Are you sure? ");


        builder2.setPositiveButton(" Yes ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getApplicationContext(), " x2 chosen - Transferring... ", Toast.LENGTH_SHORT).show();





                Handler handlerNew = new Handler();
                handlerNew.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intentMove = new Intent(MetalList.this,LoadingScreen.class);
                        startActivity(intentMove);

                    }
                },3000);






            }
        });

        builder2.setNegativeButton(" No ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.cancel();
                radioGroup.clearCheck();


            }
        });



        AlertDialog dialog = builder2.create();
        dialog.show();

    }



    if (radioButtonx3.isChecked()){


        AlertDialog.Builder builder3 = new AlertDialog.Builder(MetalList.this);
        builder3.setTitle(" Request insurance ");
        builder3.setMessage(" You selected * x3 * , Are you sure? ");


        builder3.setPositiveButton(" Yes ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getApplicationContext(), " x4 chosen - Transferring... ", Toast.LENGTH_SHORT).show();





                Handler handlerNew = new Handler();
                handlerNew.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intentMove = new Intent(MetalList.this,LoadingScreen.class);
                        startActivity(intentMove);

                    }
                },3000);






            }
        });

        builder3.setNegativeButton(" No ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.cancel();
                radioGroup.clearCheck();


            }
        });



        AlertDialog dialog = builder3.create();
        dialog.show();

    }






    if (radioButtonx4.isChecked()){


        AlertDialog.Builder builder4 = new AlertDialog.Builder(MetalList.this);
        builder4.setTitle(" Request insurance ");
        builder4.setMessage(" You selected * x5 * , Are you sure? ");


        builder4.setPositiveButton(" Yes ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(getApplicationContext(), " x5 chosen - Transferring... ", Toast.LENGTH_SHORT).show();





                Handler handlerNew = new Handler();
                handlerNew.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intentMove = new Intent(MetalList.this,LoadingScreen.class);
                        startActivity(intentMove);

                    }
                },3000);






            }
        });

        builder4.setNegativeButton(" No ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.cancel();
                radioGroup.clearCheck();


            }
        });



        AlertDialog dialog = builder4.create();
        dialog.show();

    }



}}

布局

<Button
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@drawable/brown"
    android:textSize="20dp"
    android:textStyle="bold"
    android:text="MT" />

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

<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radioGroup1">




<RadioButton
    android:text="   1"
    android:textColor="#ffd700"
    android:textStyle="bold"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_marginLeft="170dp"
    android:id="@+id/radioButton5"
    android:textSize="22dp"
    android:layout_marginTop="100dp"
    android:background="@drawable/selectordarkgreen"
    android:alpha="0"/>

<RadioButton
    android:text="   2"
    android:textColor="#c0c0c0"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:id="@+id/radioButton2"
    android:alpha="0"
    android:textSize="22dp"
    android:textStyle="bold"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/radioButton5"
    android:layout_marginLeft="170dp"
    android:background="@drawable/selectordarkgreen"/>

<RadioButton
    android:text="   3"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:id="@+id/radioButton3"
    android:layout_marginTop="20dp"
    android:textStyle="bold"
    android:alpha="0"
    android:textColor="#9b7d12"
    android:textSize="22dp"
    android:layout_below="@+id/radioButton2"
    android:layout_marginLeft="170dp"
    android:background="@drawable/selectordarkgreen"
    />


<RadioButton
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_marginTop="20dp"
    android:textSize="22dp"
    android:alpha="0"
    android:id="@+id/x4Btn"
    android:textStyle="bold"
    android:textColor="#6e2907"
    android:layout_marginLeft="170dp"
    android:layout_below="@+id/radioButton3"
    android:text="   4"
    android:background="@drawable/selectordarkgreen" />



 </RadioGroup>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="Please select the desired metal"
        android:textSize="18dp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:gravity="center"
        android:id="@+id/metalListText"
        android:alpha="0"/>


 <LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">


<Button
    android:layout_width="150dp"
    android:layout_height="60dp"
    android:background="@drawable/resultenter"
    android:text="Confirm"
    android:layout_weight="0.6"
    android:id="@+id/btnSelectMetalList"
    android:alpha="0"/>




<Button
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:text="Clear"
    android:layout_weight="0.4"
    android:background="@drawable/resultcancel"
    android:id="@+id/btnclearMetalList"
    android:alpha="0"/>


 </LinearLayout>

  </RelativeLayout>

 </LinearLayout>

2 个答案:

答案 0 :(得分:0)

除了您的代码包含大量样板文件之外,我注意到ifradioButtonx2radioButtonx3的{​​{1}}子句位于radioButtonx4之外点击监听器。

答案 1 :(得分:0)

为清楚起见,请更改:

public void onCheckedChanged(RadioGroup radioGroup, int i) {

为:

public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

然后更改表格的行:

if (radioButtonx.isChecked()){

表格中的行:

if (checkId == R.id.radioButton5) {