所以我試圖做的是有一個活動,有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>