[英]How can I create an alert dialog based of my HashMap in Android Studio?
我正在尝试根据它们的键值(int 1-6)显示一组问题,首先我会生成一个随机数,然后通过警报对话框显示与该数字相对应的问题,并以简单的响应为“OK”关闭对话框。 我试图实现一个简单的警报对话框,但它们对我来说太混乱了。
public void roll_the_dice(View view){
HashMap dbreaker = new HashMap();
dbreaker.put(1, "If you could go anywhere in the world, where would you go?");
dbreaker.put(2, "If you were stranded on a desert island, what three things would you want to take with you?");
dbreaker.put(3, "If you could eat only one food for the rest of your life, what would that be?");
dbreaker.put(4, "If you won a million dollars, what is the first thing you would buy?");
dbreaker.put(5, "If you could spaned the day with one fictional character, who would it be?");
dbreaker.put(6, "If you found a magic lantern and a genie gave you three wishes, what would you wish?");
Random r = new Random();
int dbreakerNo = r.nextInt((6-1) + 1) + 1;
}
我不确定我是否正确理解了您的问题以及实施的哪一部分让您感到困惑,但这里是代码示例,其中在按钮单击时会在警报对话框中显示随机文本。 我希望这能帮到您。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDice = findViewById(R.id.btn_dice);
btnDice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialog(getRandomQuotation());
}
});
}
private void showAlertDialog(String quoatationToShow) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Roll alert");
alertDialog.setMessage(quoatationToShow);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private String getRandomQuotation() {
HashMap<Integer, String> dbreaker = new HashMap();
dbreaker.put(1, "If you could go anywhere in the world, where would you go?");
dbreaker.put(2, "If you were stranded on a desert island, what three things would you want to take with you?");
dbreaker.put(3, "If you could eat only one food for the rest of your life, what would that be?");
dbreaker.put(4, "If you won a million dollars, what is the first thing you would buy?");
dbreaker.put(5, "If you could spaned the day with one fictional character, who would it be?");
dbreaker.put(6, "If you found a magic lantern and a genie gave you three wishes, what would you wish?");
Random r = new Random();
int dbreakerNo = r.nextInt((6 - 1) + 1) + 1;
return dbreaker.get(dbreakerNo);
}
}
activity_main.xml <-- 这是你的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_dice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll the dice!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.