我正在尝试从选择弹出菜单项后弹出的对话框中获取输入。我不太明白如何获取用户的文本输入,以便我可以将其设置为我的TextView“ListName”(参见最后一个方法)。我得到的文字是: android.widget.EditText@41f3abf8
MainActivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Method for activity events
public void onButtonClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG:
createLGPopup(v);
break;
case R.id.EditButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createEditButtonPopup(v);
break;
}
}
// findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
// TextView text = (TextView) findViewById(R.id.GetStarted);
// text.setText(choice);
// CreateLG Button's Popup Menu
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createlg_menu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice.equals("Create List")) {
createListDialog();
}
else if (choice.equals("Create Group")) {
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
// Create Edit Button's Popup Menu
public void createEditButtonPopup(View v) {
PopupMenu EditMenu = new PopupMenu(this, v);
EditMenu.getMenuInflater().inflate(R.menu.editlist_menu, EditMenu.getMenu());
EditMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice.equals("Edit List Name")) {
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
//createListDialog();
}
else if (choice.equals("Clear List Items")) {
findViewById(R.id.GetStarted).setVisibility(View.VISIBLE);
//createListDialog();
}
else if (choice.equals("Delete List Name")) {
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
//createListDialog();
}
return false;
}
});
EditMenu.show();
}
// Create List Dialog
public AlertDialog.Builder dialogBuilder;
private void createListDialog() {
dialogBuilder = new AlertDialog.Builder(this);
final EditText textInput = new EditText(this);
dialogBuilder.setTitle("Create list");
dialogBuilder.setMessage("Name your list: ");
dialogBuilder.setView(textInput);
dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView text = (TextView) findViewById(R.id.ListName);
text.setText(textInput.toString());
//TextView text = (TextView) findViewById(R.id.ListName);
//String input = textInput.getText().toString(); //(textInput.toString());
//Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT);
// add list to ListsButton
//findViewById(R.id.ListName). -> Change ListName text to created list
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT);
}
});
// Output
AlertDialog dialogue = dialogBuilder.create();
dialogue.show();
}
}
答案 0 :(得分:1)
根据评论中讨论的内容,您应该能够像这样:
@Override
public void onClick(DialogInterface dialog, int which) {
TextView text = (TextView) findViewById(R.id.ListName);
text.setText(textInput.getText().toString());
// ...
ListName
),findViewById()
,因为对话框被定义为活动的嵌套类,因此findViewById()
已转换为MainActivity.this.findViewById()
,因此应该ListName
很好。它还必须被强制转换为使用特定于TextView
类的方法。textInput.toString()
,则会调用toString()
对象的TextView
方法。看起来TextView
不会覆盖toString()
,所以这是默认的Object#toString()
方法,它只返回对象类型的名称加上其哈希码(这就是为什么你看到{ {1}})。您需要先从视图中获取android.widget.EditText@41f3abf8
(CharacterSequence
)。答案 1 :(得分:0)
调用findViewById时必须使用对话框的上下文。像这个dialog.findViewById(R.id.ListName),而不仅仅是findViewById(R.id.ListName)。出于这个原因,该对话框作为参数传递给onClick()方法。
答案 2 :(得分:0)
在onClick中添加此内容:text.setText(input);