1
我是android新手。我正在開發一個考試應用程序,我想在對話框中顯示所有問題編號,以便用戶可以切換到任何問題,即1,2,3,4等。我嘗試了下面的代碼,但它不正確。我從服務器示例中得到了全部的問題,我有40個問題,所以我想在對話框中顯示1到40之間的數字。請幫忙在對話框中添加數字
AlertDialog.Builder alert = new AlertDialog.Builder(context);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
for (int i = 0; i < 20; i++) {
// Set an EditText view to get user input
final TextView input = new TextView(context);
input.setText("" + i);
input.setPadding(5, 5, 5, 5);
input.setTextSize(15);
input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String srt = "clickd";
Toast.makeText(context, srt, Toast.LENGTH_LONG).show();
}
});
layout.addView(input);
alert.setView(layout);
}
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//You will get as string input data in this variable.
// here we convert the input to a string and show in a toast.
String srt = "fdsfdsf";
Toast.makeText(context, srt, Toast.LENGTH_LONG).show();
} // End of onClick(DialogInterface dialog, int whichButton)
}); //End of alert.setPositiveButton
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
}
}); //End of alert.setNegativeButton
AlertDialog alertDialog = alert.create();
alertDialog.show();
我已經在這裏提出了20個問題,但它可以是任何沒有。這樣
爲什麼你不使用對話框內的列表視圖 –
@VishalGaur我發佈了一張圖片。請檢查。我想要這樣的東西 – Priyanka
你應該使用對話框片段並創建GridView佈局 –