Android- 动态获取用户文本并将其添加到 table 布局
Android- dynamically get text from user and add it to table layout
我是 Android 开发的新手,遇到了一些问题。
我正在构建一个应用程序,我想在其中为用户弹出一个对话框,从对话框中获取文本并将其动态添加为预定义 table 布局中的单元格。我想多次执行此操作,每次单击一个按钮。
如果这是重复的,我深表歉意,但我在网上搜索了很多解决方案,但没有找到适合我的解决方案。
我认为我的主要问题是 AlertDialog.Builder 的异步,我的代码不会等待它,因为我无法阻止 UI 线程。所以我尝试设置一个监听器,但这似乎也不起作用。
非常感谢Android中的任何帮助或关于听众的一些基本知识。
谢谢!
在class中定义的实例变量:
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);
}
创建一个接口,例如调用它动作
public interface Action {
public void onCompleted(String input);
}
更改您的方法签名
public void showDialog(String message, final Context context, final Action action)
更改正面按钮的 onClick 方法。
public void onClick(DialogInterface dialog, int whichButton) {
userInput = input.getText().toString();
if(action!=null) action.onCompleted(userInput);
}
那么例如创建一个匿名 class
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);
}
});
您可以通过为您的对话框创建一个专用 class 来稍微清理一下。
尝试在您的 Activity
中添加一个更新 TableLayout
的方法
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()
中调用它:
alertBox.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addRow(userInput);
}
});
我是 Android 开发的新手,遇到了一些问题。 我正在构建一个应用程序,我想在其中为用户弹出一个对话框,从对话框中获取文本并将其动态添加为预定义 table 布局中的单元格。我想多次执行此操作,每次单击一个按钮。
如果这是重复的,我深表歉意,但我在网上搜索了很多解决方案,但没有找到适合我的解决方案。 我认为我的主要问题是 AlertDialog.Builder 的异步,我的代码不会等待它,因为我无法阻止 UI 线程。所以我尝试设置一个监听器,但这似乎也不起作用。
非常感谢Android中的任何帮助或关于听众的一些基本知识。
谢谢!
在class中定义的实例变量:
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);
}
创建一个接口,例如调用它动作
public interface Action {
public void onCompleted(String input);
}
更改您的方法签名
public void showDialog(String message, final Context context, final Action action)
更改正面按钮的 onClick 方法。
public void onClick(DialogInterface dialog, int whichButton) {
userInput = input.getText().toString();
if(action!=null) action.onCompleted(userInput);
}
那么例如创建一个匿名 class
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);
}
});
您可以通过为您的对话框创建一个专用 class 来稍微清理一下。
尝试在您的 Activity
中添加一个更新 TableLayout
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()
中调用它:
alertBox.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addRow(userInput);
}
});