简体   繁体   English

从Dialog获取数据并将其发布到MainActivity

[英]Get data from Dialog and post it to MainActivity

I am new to android. 我是android的新手。 I have created a Dialog and in this Dialog I have 2 EditText fields. 我创建了一个Dialog ,在这个Dialog我有2个EditText字段。

I want get the values(user entered) from this 2 EditText fields and show it to the 2 separate TextView fields in MainActivity But no matter how much I try, I can't get the value from EditText fields. 我想从这2个EditText字段中获取值(用户输入)并将其显示到MainActivity的2个单独的TextView字段但是无论我尝试多少,我都无法从EditText字段中获取值。

Please help. 请帮忙。 The code I have till now... 我现在的代码......

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;
        }

    }
}

Replcae your onClick method with this. 用这个重新编写你的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;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM