[英]Android- dynamically get text from user and add it to table layout
我是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)
更改您的肯定按鈕onClick方法。
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);
}
});
您可以通過為對話框創建一個專用的類來對其進行清理。
嘗試在您的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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.