在对话框中添加数字
Add numbers in dialog box
我是 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道题了,可以随便选no。像这样
似乎您正在尝试显示固定大小的数据,比方说 response.Simple 中的 20 个数字是在您的 Dialog 中设置 gridView 并使用固定数量的数据,如下所示:
private void showDialog() {
//Here is ur gridview
GridView gridView = new GridView(ctx);
List<Integer> mList = new ArrayList<Integer>();
for (int i = 1 ; i < 20; i++) {
mList.add(i);
}
gridView.setAdapter(new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, mList));
gridView.setNumColumns(4);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do what ev you want with ur stuff here
}
});
// Here you can set grid view to ur dialog
AlertDialog.Builder builder = new AlertDialog.Builder(ctxI);
builder.setView(gridView);
builder.setTitle("Its me !!");
builder.show();
}
我是 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道题了,可以随便选no。像这样
似乎您正在尝试显示固定大小的数据,比方说 response.Simple 中的 20 个数字是在您的 Dialog 中设置 gridView 并使用固定数量的数据,如下所示:
private void showDialog() {
//Here is ur gridview
GridView gridView = new GridView(ctx);
List<Integer> mList = new ArrayList<Integer>();
for (int i = 1 ; i < 20; i++) {
mList.add(i);
}
gridView.setAdapter(new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, mList));
gridView.setNumColumns(4);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do what ev you want with ur stuff here
}
});
// Here you can set grid view to ur dialog
AlertDialog.Builder builder = new AlertDialog.Builder(ctxI);
builder.setView(gridView);
builder.setTitle("Its me !!");
builder.show();
}