簡體   English   中英

如何在 Android Studio 中基於我的 HashMap 創建警報對話框?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM