Android-从用户处动态获取文本并将其添加到表格布局中

Android-从用户处动态获取文本并将其添加到表格布局中,android,android-alertdialog,Android,Android Alertdialog,我是Android开发的新手,遇到了一些问题。 我正在构建一个应用程序,希望在其中为用户弹出一个对话框,从对话框中获取文本,并将其动态添加为预定义表格布局中的单元格。我想做多次,每次点击一个按钮 如果这是重复的话,我很抱歉,但是我在网上搜索了很多解决方案,但都没有找到适合我的解决方案。 我认为我的主要问题是AlertDialog.Builder的异步,我的代码不会等待它,因为我无法阻止UI线程。所以我试着设置一个听众,但这似乎也不起作用 我们将非常感谢您对Android系统中的听众提供的任何帮助

我是Android开发的新手,遇到了一些问题。 我正在构建一个应用程序,希望在其中为用户弹出一个对话框,从对话框中获取文本,并将其动态添加为预定义表格布局中的单元格。我想做多次,每次点击一个按钮

如果这是重复的话,我很抱歉,但是我在网上搜索了很多解决方案,但都没有找到适合我的解决方案。 我认为我的主要问题是AlertDialog.Builder的异步,我的代码不会等待它,因为我无法阻止UI线程。所以我试着设置一个听众,但这似乎也不起作用

我们将非常感谢您对Android系统中的听众提供的任何帮助或一些基本知识

谢谢

类中定义的实例变量:

String userInput;
显示对话框的方法:

 public void showDialog(String message, final Context context) {
    AlertDialog.Builder alertBox = new AlertDialog.Builder(context);
    alertBox.setMessage(message);
    final EditText input = new EditText(this);
    alertBox.setView(input);
    alertBox.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    userInput = input.getText().toString();
                }
            });
    alertBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alertBox.show();
}
通过单击按钮调用对话框并设置新行:

    public void addTeam(View view) {
    showDialog("Please enter text", this);
    //Here I want to already have userInput var, thus it should be after the click.

    TableRow row= new TableRow(this);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);
    TextView name = new TextView(this);
    name.setText(userInput);
    name.setTextSize(20);
    name.setPaintFlags(name.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    row.addView(name);
    //teamsTable is a TableLayout defined int the XML
    teamsTable.addView(row, ++nTeamTableRowCount);
}

创建一个接口,调用它,例如Action

public interface Action {

    public void onCompleted(String input);

}
更改您的方法签名

public void showDialog(String message, final Context context, final Action action) 
改变你的正面按钮点击方法

public void onClick(DialogInterface dialog, int whichButton) {
    userInput = input.getText().toString();        
    if(action!=null) action.onCompleted(userInput);
}
然后,例如,创建一个匿名类

 showDialog("Please enter text", this, new Action() {

      public void onCompleted(String input) {

          //Do what you want with input here. e.g.

          TableRow row= new TableRow(this);
          TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
          row.setLayoutParams(lp);
          TextView name = new TextView(this);
          name.setText(userInput);
          name.setTextSize(20);
          name.setPaintFlags(name.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
          row.addView(name);
          //teamsTable is a TableLayout defined int the XML
          teamsTable.addView(row, ++nTeamTableRowCount);

      }

 });

您可以通过为对话框创建一个专用类来清理它。

尝试在
活动中添加一个更新
表格布局的方法

private void addRow(String userInput) {
    TableRow row = new TableRow(this);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(lp);
    TextView name = new TextView(this);
    name.setText(userInput);
    name.setTextSize(20);
    name.setPaintFlags(name.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    row.addView(name);
    teamsTable.addView(row, ++nTeamTableRowCount);
}
并在
onClick()中调用它:


一切都发生在ui线程上。那么阻塞ui线程从何而来?不确定你的意思。我的方法是从活动中设置一个对话框并使用文本输入。这是错误的吗?除非您有一个线程(后台)线程,否则所有操作都在ui线程上。只有在希望将工作卸载到后台线程时,才创建线程。那么,你文章中的bloacking ui线程来自哪里?我没有创建任何阻塞线程,是否有默认ui线程?谢谢你的回答!虽然@brescia123为我解决了这个问题,但我可以知道什么是“行动”类吗?操作从何处返回,如果为null会发生什么?这是否意味着单击丢失?您应该阅读接口和松耦合。然后你会发现brescia123的答案不是最好的。使用接口,您可以添加另一层抽象。使用brescia123 answer,您的按钮与您的活动相关联,并且始终会执行相同的操作。使用我的答案,您可以通过创建不同的操作来控制按钮的操作,并且您甚至可以在其他活动中使用您的对话框。效果非常好:)非常感谢!
alertBox.setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            addRow(userInput);
        }
    });