如何存储Android对话框中的编辑文本数据?

时间:2015-01-04 01:36:40

标签: android android-edittext android-alertdialog layout-inflater

我已经设置了一个包含多个编辑文本的警告对话框,但我不确定如何存储在警告对话框中输入的值。

通常我可以按照以下方式做点什么:

final EditText input = new EditText(this);
alert.setView(input);
Editable value = input.getText();

但我的MessageDialog是一个像SearchResult.java这样调用的独立类,所以我不知道如何访问MyMessageDialog.java中编辑文本的实例:

MyMessageDialog.displayMessage(SearchResult.this, "Sample Info", "Required");

有谁知道如何在此实现中检索编辑文本值?

这是MyMessageDialog类,下面是警告对话框的布局:

public class MyMessageDialog  {

    @SuppressLint("NewApi") 
    public static AlertDialog displayMessage(Context context, String title, String message){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    LayoutInflater inflater = LayoutInflater.from(context);
    builder.setTitle(title); 
    builder.setMessage(message); 
    builder.setView(inflater.inflate(R.layout.custom_view, null));
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    dialog.cancel(); 
    } 
    }); 
    builder.show(); 
    return builder.create(); 
    } 

}

警报对话框布局,custom_view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ship name"
        android:id="@+id/shipNameEditText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="analyst name"
        android:id="@+id/scientistEditText2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email address"
        android:id="@+id/emailEditText3"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="sample volume"
        android:id="@+id/volumeEditText4" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="sample colour"
        android:id="@+id/colourEditText4" />


</LinearLayout>

1 个答案:

答案 0 :(得分:4)

向MyMessageDialog类添加一个接口以传回值:

    public interface MyMessageDialogListener {
        public void onClosed(String ship, String scientist, String email, String volume, String color);
    }

在创建对话框布局时存储对话框布局并提取EditText值并通过OK按钮内的侦听器将它们传回onClick:

public class MyMessageDialog  {

    public interface MyMessageDialogListener {
        public void onClosed(String ship, String scientist, String email, String volume,     String color);
    }

@SuppressLint("NewApi") 
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    LayoutInflater inflater = LayoutInflater.from(context);
    builder.setTitle(title); 
    builder.setMessage(message); 
    final View layoutView = inflater.inflate(R.layout.custom_view, null);
    builder.setView(layoutView);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

    // get the edit text values here and pass them back via the listener
    if(listener != null)
    {
        EditText text1 = (EditText)layoutView.findViewById(R.id.shipNameEditText);
        EditText text2 = (EditText)layoutView.findViewById(R.id.scientistEditText2);
        EditText text3 = (EditText)layoutView.findViewById(R.id.emailEditText3);
        EditText text4 = (EditText)layoutView.findViewById(R.id.volumeEditText4);
        EditText text5 = (EditText)layoutView.findViewById(R.id.colourEditText4);

        listener.onClosed(text1.getText().toString(),
            text2.getText().toString(),
            text3.getText().toString(),
            text4.getText().toString(),
            text5.getText().toString());
        }

        dialog.cancel(); 
    } 
    }); 
    builder.show(); 
    return builder.create(); 
    } 

}

当您调用对话框并使用它来接收字符串时,创建一个侦听器实例:

MyMessageDialog.displayMessage(SearchResult.this, "Sample Info", "Required",
    new MyMessageDialog.MyMessageDialogListener() {
        public void onClosed(String ship, String scientist, String email, String volume, String color)
        {
            // store / use the values here
        }
    });