Java 从对话框中获取数据并将其发布到MainActivity

Java 从对话框中获取数据并将其发布到MainActivity,java,android,Java,Android,我是android新手。我已经创建了一个对话框,在这个对话框中,我有2个编辑文本字段 我想从这2个EditText字段中获取值(用户输入),并将其显示到MainActivity中的2个单独的TextView字段,但无论我如何尝试,我都无法从EditText字段中获取值 请帮忙。我到现在为止的代码 package edu.arnab.simpledialogmenu; import android.app.AlertDialog; import android.content.DialogInt

我是android新手。我已经创建了一个
对话框
,在这个
对话框
中,我有2个
编辑文本
字段

我想从这2个
EditText
字段中获取值(用户输入),并将其显示到
MainActivity
中的2个单独的
TextView
字段,但无论我如何尝试,我都无法从
EditText
字段中获取值

请帮忙。我到现在为止的代码

package edu.arnab.simpledialogmenu;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements OnClickListener {

    RelativeLayout layout;
    TextView tvTitle, tvCaption, tvStudio;
    EditText gTitle, gCaption;
    String title, caption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout = (RelativeLayout) findViewById(R.id.relative1);

        tvTitle = (TextView) findViewById(R.id.textView1);
        tvCaption = (TextView) findViewById(R.id.textView2);
        tvStudio = (TextView) findViewById(R.id.textView3);

        registerForContextMenu(layout);
        registerForContextMenu(tvTitle);

    }

    @Override

    public void onCreateContextMenu(android.view.ContextMenu menu, android.view.View v, android.view.ContextMenu.ContextMenuInfo menuInfo) 
    {
        if(v == layout)
        {
            menu.add(1, 1, 0, "Make Background Yellow");
            menu.add(1, 2, 0, "Make Background Cyan");
        }
        else if(v == tvTitle)
        {

            menu.removeGroup(1);
            menu.add(2, 3, 0, "Make Title COD");
            menu.add(2, 4, 0, "Make Title NFS");
        }

        super.onCreateContextMenu(menu, v, menuInfo);
    };

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch(item.getItemId())
        {
            case 1:
                layout.setBackgroundColor(Color.YELLOW);
                break;
            case 2:
                layout.setBackgroundColor(Color.CYAN);
                break;
            case 3:
                tvTitle.setText("Title: Call of Duty");
                break;
            case 4:
                tvTitle.setText("Title: Need for Speed");
                break;
        }
        return super.onContextItemSelected(item);
    }

    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(1, 1, 1, "Game Entry Dialog");
        menu.add(1, 2, 1, "Change Background");
        menu.add(1, 3, 1, "Exit App");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String text =  null;
        switch(item.getItemId())
        {
            case 1:
                text = item.getTitle().toString();
                // Show Game Dialog
                AlertDialog.Builder ab = new AlertDialog.Builder(this);
                ab.setTitle("New Game Entry Dialog");

                View view = getLayoutInflater().inflate(R.layout.dlg_layout, null);
                ab.setView(view);

                gTitle = (EditText) view.findViewById(R.id.editTitle);
                gCaption = (EditText) view.findViewById(R.id.editCaption);
                title = gTitle.getText().toString();
                caption = gCaption.getText().toString();



                ab.setPositiveButton("OKAY IT", this);
                ab.setNegativeButton("CANCEL IT", this);

                AlertDialog ad = ab.create();
                ad.show();

                break;
            case 2:
                text = item.getTitle().toString();

                layout.setBackgroundColor(Color.GREEN);
                tvTitle.setBackgroundColor(Color.WHITE);
                tvCaption.setBackgroundColor(Color.LTGRAY);
                break;
            case 3:
                text = item.getTitle().toString();

                finish();
                break;
        }

        Toast.makeText(this, "You have selected menu item " + text, 3000).show();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub

        switch(arg1)
        {
            case DialogInterface.BUTTON_POSITIVE:
                //take text from dialog fields and show all info on MainActivity
                tvTitle.setText(title);
                tvCaption.setText(caption);
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                Toast.makeText(this, "You cancelled dialog entry", 300).show();
                break;
        }

    }
}

用此命令重新单击
onClick
方法

@Override
    public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub

        switch(arg1)
        {
            case DialogInterface.BUTTON_POSITIVE:
                //take text from dialog fields and show all info on MainActivity
                //these are changes
                title = gTitle.getText().toString(); 
                caption = gCaption.getText().toString();

                tvTitle.setText(title);
                tvCaption.setText(caption);
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                Toast.makeText(this, "You cancelled dialog entry", 300).show();
                break;
        }

    }

简单地说,您也可以使用接口和事件总线“我无法从EditText字段获取值。”为什么?是什么阻止了你?编写以下代码
title=gTitle.getText().toString();caption=gCaption.getText().toString()
on
onClick
method of
BUTTON\u POSITIVE
@Kuffs与我的代码到目前为止,我只得到空白值。现在变量名
title
caption
@aksacha中有一个值,我测试过你的代码。当我将代码移动到
onclick
按钮的
方法中时,我得到错误
视图未定义
。如果您发现此答案有帮助,您可以向上投票,以便其他人可以从中获得帮助。我已向上投票您的答案,但我收到以下通知:感谢反馈!声誉低于15的人所投的票将被记录,但不会改变公开显示的帖子分数。对不起,没问题。。快乐编码:)