0

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

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

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();


}
4

1 回答 1

0

You can prompt a dialog for each item in the for loop and get user input

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;
}
于 2016-05-24T09:19:45.333 回答