在android中等效于for循环的文本框数量

时间:2016-05-24 09:07:22

标签: android for-loop button user-input editbox

我想根据for循环生成一些editbox,其中一个按钮必须调用将用户输入数据处理到编辑框的方法。

据我所知,我不能使用警报对话框,因为它们是异步的,不会等待用户响应,我可能会使用上面提到的方法并查看行为。 并提出建议和意见。下面的代码可能会提供更多的想法。

for(int x=0;x<vars.size();x++)
    {
        String get_var=vars.get(x).toString();//get the first item from array
        replace(get_var,temp_t);//send the get_var to replace method
    }

替换方法的想法是

replace(String get_var,String temp_t)
{
 String y=(user input from the textbox, which has to be created dynamically)
if(button pressed) //created dynamically
process(y,temp_t)
}

请建议是否有任何其他工作更好,然后我认为应该工作,如消除按钮和直接处理 [更新]

    for(int x=0;x<vars.size();x++)
    {
        String get_var=vars.get(x).toString();
        get_vals_for_xy(get_var, temp_t);
    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}//end of oncreate method

public void get_vals_for_xy(String get_var,String[][] temp_t) {
    System.out.println("value of get_var is " + get_var);
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final EditText edittext = new EditText(this);
    alert.setMessage("value is"+get_var);
    alert.setTitle("Title");

    alert.setView(edittext);

    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            String to_replace = edittext.getText().toString();
            show(to_replace);
            Toast.makeText(AssignValandFacts.this, to_replace, Toast.LENGTH_SHORT).show();
        }
    });

    alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });

    alert.show();


}

1 个答案:

答案 0 :(得分:0)

您可以为for循环中的每个项目提示对话框并获取用户输入

boolean userResponseReceived=false;
//create a dialog
Dialog dialog=new Dialog(context);
//set dialog content view
//I just assume you have a button in dialog that says OK
Button okButton=(Button)dialog.findViewById(R.id.btnId);
okButton.setOnClickListener(new View.OnclickListener{
     @Override
     public void onClick(View v){
         //userResponseRecieved is a instance variable
         userResponseReceived=true;
     }
});
for(int x=0;x<vars.size();x++){
    //make a while loop to hold the for loop while user inserts data
    //make a dialog box and handle user input
    dialog.show();
    while(!userResponseReceived){}
    //continue with the for loop
    dialog.dismiss();
    userResponseReceived=false;
}