使用AlertDialog同步获取文本
如何使用AlertDialog同步获取文本?例如,我想做这样的事情:
public String GetTextDialog()
{
final EditText text = new EditText(activity);
// Gets the chat
final AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
dialog.setView(text);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
// Display
dialog.show();
// Return text after dialog is complete
return text.getText().toString();
}
编辑:你认为这是一个更好的方法吗:
interface TextHandler
{
public String Title();
public void HandleText(String text);
}
public static boolean ShowTextDialog(
String title,
String defaultValue,
final TextHandler posButton,
final TextHandler negButton,
final TextHandler neutButton,
int width
)
{
// Check for already existing dialog
if(showDialog) return false;
showDialog = true;
// Verify context
if(context == null)
{
Log.e("Crystal", "Screen::ShowTextDialog No Context!");
showDialog = false;
return false;
}
// Verify we have buttons
if(neutButton == null)
{
if(posButton == null || negButton == null)
{
Log.e("Crystal", "Screen::ShowTextDialog Must supply both postive and negative buttons!");
showDialog = false;
return false;
}
}
else
{
if(posButton != null || negButton != null)
{
Log.e("Crystal", "Screen::ShowTextDialog Can't have neutral button with other type!");
showDialog = false;
return false;
}
}
// Create the dialog
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
if(title != null) dialog.setTitle(title);
// Create the chat
final EditText text = new EditText(context);
text.setSingleLine();
if(defaultValue != null) text.setText(defaultValue);
if(width != 0) text.setWidth((int)(width*context.getResources().getDisplayMetrics().density));
// Add text to dialog
dialog.setView(text);
if(posButton != null)
{
dialog.setPositiveButton(posButton.Title(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
posButton.HandleText(text.getText().toString());
showDialog = false;
}
});
}
if(negButton != null)
{
dialog.setNegativeButton(negButton.Title(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
negButton.HandleText(text.getText().toString());
showDialog = false;
}
});
}
if(neutButton != null)
{
dialog.setNeutralButton(neutButton.Title(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
neutButton.HandleText(text.getText().toString());
showDialog = false;
}
});
}
// Display
dialog.show();
return true;
}
并这样调用:
private void GetUsernameDialog()
{
ShowTextDialog(
"Enter Username", // Title
username, // Default value
new TextHandler() // Positive button
{
public String Title() { return "Ok"; }
public void HandleText(String text) { SetUsername(text); }
},
new TextHandler() // Negative button
{
public String Title() { return "Cancel"; }
public void HandleText(String text) { }
},
null, // Neutral button
200 // Width
);
}
How do I use AlertDialog to get text synchronously? For instance, I would want to do something like this:
public String GetTextDialog()
{
final EditText text = new EditText(activity);
// Gets the chat
final AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
dialog.setView(text);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
// Display
dialog.show();
// Return text after dialog is complete
return text.getText().toString();
}
EDIT: Would you consider this a better way:
interface TextHandler
{
public String Title();
public void HandleText(String text);
}
public static boolean ShowTextDialog(
String title,
String defaultValue,
final TextHandler posButton,
final TextHandler negButton,
final TextHandler neutButton,
int width
)
{
// Check for already existing dialog
if(showDialog) return false;
showDialog = true;
// Verify context
if(context == null)
{
Log.e("Crystal", "Screen::ShowTextDialog No Context!");
showDialog = false;
return false;
}
// Verify we have buttons
if(neutButton == null)
{
if(posButton == null || negButton == null)
{
Log.e("Crystal", "Screen::ShowTextDialog Must supply both postive and negative buttons!");
showDialog = false;
return false;
}
}
else
{
if(posButton != null || negButton != null)
{
Log.e("Crystal", "Screen::ShowTextDialog Can't have neutral button with other type!");
showDialog = false;
return false;
}
}
// Create the dialog
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
if(title != null) dialog.setTitle(title);
// Create the chat
final EditText text = new EditText(context);
text.setSingleLine();
if(defaultValue != null) text.setText(defaultValue);
if(width != 0) text.setWidth((int)(width*context.getResources().getDisplayMetrics().density));
// Add text to dialog
dialog.setView(text);
if(posButton != null)
{
dialog.setPositiveButton(posButton.Title(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
posButton.HandleText(text.getText().toString());
showDialog = false;
}
});
}
if(negButton != null)
{
dialog.setNegativeButton(negButton.Title(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
negButton.HandleText(text.getText().toString());
showDialog = false;
}
});
}
if(neutButton != null)
{
dialog.setNeutralButton(neutButton.Title(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
neutButton.HandleText(text.getText().toString());
showDialog = false;
}
});
}
// Display
dialog.show();
return true;
}
And called like this:
private void GetUsernameDialog()
{
ShowTextDialog(
"Enter Username", // Title
username, // Default value
new TextHandler() // Positive button
{
public String Title() { return "Ok"; }
public void HandleText(String text) { SetUsername(text); }
},
new TextHandler() // Negative button
{
public String Title() { return "Cancel"; }
public void HandleText(String text) { }
},
null, // Neutral button
200 // Width
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以在一个带有 Looper 的替代线程中运行对话框,在对话框关闭时终止 Looper,让主 UI 线程等待另一个线程......但这是一个如此复杂的设计,上帝可能会杀死一只小猫,如果你就这么做。请问为什么需要同步数据访问?这只是为了方便,还是有设计原因?
如果它是关于访问对话框设置之前的状态,那么嵌套匿名类就是您的答案。您已经在上面的代码片段中获得了这些;它们可以自动访问嵌套函数的
final
变量以及嵌套类的成员。You can run the dialog in an alternative thread with a Looper in it, terminating the looper on dialog dismissal, having the main UI thread wait on that other one... but this is such a convoluted design, God might just kill a kitten if you do that. May I ask why do you need synchronous data access? Is this just about convenience, or you have a design reason?
If it's about accessing state from before the dialog setup, then nested anonymous classes are your answer. You already have those in the snippet above; they have automagical access to the
final
variables of the nesting function and to members of the nesting class.