2017-07-28 11 views
0

私が作ろうとしているのは、ラジオボタンとその下の確認ボタンで作成された4つのオプションがあるアクティビティです。私がしようとしているのは、それらの4つのオプションのうちの1つをクリックして「確認」ボタンをクリックすると、アラートダイアログが表示され、顧客に「x2を選択しますか、本当ですか?」という質問が表示されます。ラジオボタンとアラートダイアログを結合する方法

私は遠く離れて作ったが、それは最初の1つの "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> 

答えて

0

はたくさんの定型の含まれている、それはradioButtonx2radioButtonx3radioButtonx4のものif句はbtnSelectクリックリスナー外であることを私の注意に来ました。次いで

public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { 

形式の行を変更:明瞭、変更の

0

public void onCheckedChanged(RadioGroup radioGroup, int i) { 

if (radioButtonx.isChecked()){ 

フォームの行に:

if (checkId == R.id.radioButton5) { 
関連する問題