Android 在对话框中添加数字

Android 在对话框中添加数字,android,android-alertdialog,Android,Android Alertdialog,我是android新手。我正在开发一个考试应用程序,其中我希望在对话框中显示所有问题编号,以便用户可以切换到任何问题,如1、2、3、4等。我尝试了以下代码,但不正确。我从服务器得到的问题总数为:例如,我有40个问题,所以我想在对话框中显示从1到40的数字。请帮忙 AlertDialog.Builder alert = new AlertDialog.Builder(context); LinearLayout layout = new LinearLayout(th

我是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();
AlertDialog.Builder alert=新建AlertDialog.Builder(上下文);
LinearLayout布局=新的LinearLayout(本);
布局。设置方向(线性布局。水平);
layout.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_父项,LinearLayout.LayoutParams.FILL_父项));
对于(int i=0;i<20;i++){
//设置EditText视图以获取用户输入
最终文本视图输入=新文本视图(上下文);
输入.setText(“+i”);
设置填充(5,5,5,5);
输入.setTextSize(15);
input.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串srt=“clickd”;
Toast.makeText(上下文、srt、Toast.LENGTH_LONG).show();
}
});
layout.addView(输入);
alert.setView(布局);
}
alert.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
//您将在此变量中获得字符串输入数据。
//在这里,我们将输入转换为字符串并显示在toast中。
字符串srt=“fdsfdsf”;
Toast.makeText(上下文、srt、Toast.LENGTH_LONG).show();
}//onClick(DialogInterface对话框,int whichButton)结束
}); //alert.setPositiveButton的结束
alert.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(对话框接口对话框,int whichButton){
//取消了。
dialog.cancel();
}
}); //alert.setNegativeButton的结束
AlertDialog AlertDialog=alert.create();
alertDialog.show();
我在这里回答了20个问题,但可以是任何一个不,像这样的问题


似乎您试图显示固定大小的数据,比如说响应中的20个数字。简单的方法是在对话框中使用固定数量的数据设置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();
}
private void showDialog(){
//这是您的gridview
GridView GridView=新的GridView(ctx);
List mList=new ArrayList();
对于(int i=1;i<20;i++){
M列表添加(i);
}
setAdapter(新的ArrayAdapter(ctx,android.R.layout.simple_list_item_1,mList));
gridView.setNumColumns(4);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//你想用你的东西做什么
}
});
//在这里,您可以将栅格视图设置为ur对话框
AlertDialog.Builder=新建AlertDialog.Builder(ctxI);
builder.setView(gridView);
builder.setTitle(“它是我!!”);
builder.show();
}

为什么不在dialog@VishalGaur我贴了一张图片。请查收。我想要这样的东西你应该使用对话框片段并创建GridView布局