gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:34:12 26 4
gpt4 key购买 nike

我是安卓新手。我创建了一个 Dialog 并且在这个 Dialog 中我有 2 个 EditText 字段。

我想从这 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;
}

}

关于java - 从 Dialog 中获取数据并将其发布到 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42437989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com