Android将数据从对话框传递到弹出对话框的屏幕

时间:2012-05-19 08:58:17

标签: android dialog

我想通过对话框填充主屏幕上的编辑文本。弹出对话框。用户将输入数据(名称或电子邮件),然后单击确定。应在显示对话框的屏幕上显示名称和电子邮件..

public class DialogActivity extends Activity 
{
/* Called when the activity is first created.*/
final Context context = this;
//private Button button;
String result;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item);


        final Dialog dialog = new Dialog(DialogActivity.this);
        dialog.setContentView(R.layout.main);
        dialog.setTitle("I'm soo ");
        dialog.setCancelable(true);
        dialog.show();

        Button okButton = (Button) dialog.findViewById(R.id.button1);
        okButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

        LayoutInflater factory = LayoutInflater.from(DialogActivity.this);
                    final EditText barFirst= (EditText) dialog.findViewById(R.id.edittext01);
                    final EditText barSecond= (EditText) dialog.findViewById(R.id.edittext02);
                    result = barFirst.getText().toString();

                    dialog.hide(); 

            }  
        });
      EditText tview = (EditText)findViewById(R.id.cat_id);
        tview.setText(result);
        Toast.makeText(this, result,Toast.LENGTH_LONG).show();

    }
}

我的xml文件必须EditText想要在对话框关闭后显示对话框中输入的数据

1 个答案:

答案 0 :(得分:1)

 public class DialogActivity extends Activity {

/* Called when the activity is first created.*/
   final Context context = this;
    //private Button button;
       String result;

       @Override
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

          setContentView(R.layout.main);




    final Dialog dialog = new Dialog(DialogActivity.this);
    dialog.setContentView(R.layout.main);
    dialog.setTitle("I'm soo ");
    dialog.setCancelable(true);
    dialog.show();

    Button okButton = (Button) dialog.findViewById(R.id.button1);
    okButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

    LayoutInflater factory = LayoutInflater.from(DialogActivity.this);
                final EditText barFirst= (EditText) dialog.findViewById(R.id.edittext01);
                final EditText barSecond= (EditText) dialog.findViewById(R.id.edittext02);
                result = barFirst.getText().toString();

                dialog.dismiss(); 
    EditText tview = (EditText)findViewById(R.id.cat_id);
    tview.setText(result);



        }  
    });