如何在Android Studio中基于我的HashMap创建警报对话框?

我正在尝试根据其键值int 1-6显示一组问题,首先,我将生成一个随机数,然后通过带有简单响应的警报对话框显示与该数字相对应的问题,例如:单击“确定”关闭对话框。我试图实现一个简单的警报对话框,但是它们对我来说太混乱了。

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;


}
zhangcy_77 回答:如何在Android Studio中基于我的HashMap创建警报对话框?

我不确定我是否正确理解了您的问题,以及实现的哪一部分使您感到困惑,但这是代码示例,其中在单击按钮时在警报对话框中显示随机文本。我希望这能帮到您。

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>
本文链接:https://www.f2er.com/3142107.html

大家都在问