gpt4 book ai didi

android - 从 alertDialog 构建器获取 editText 值

转载 作者:行者123 更新时间:2023-11-29 15:49:26 27 4
gpt4 key购买 nike

我是 android 的新手。我需要获取 editText 的 值来搜索某些内容。但是当我运行程序时,出现了null pointer exception 错误。

“EditText etSearch”没有获取它的文本。

请帮帮我。谢谢。

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_search) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
// Get the layout inflater
LayoutInflater inflater = MainActivity.this.getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog
// layout
builder.setView(inflater.inflate(R.layout.dialog_save, null))

// Add action buttons
.setPositiveButton("Search",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
final EditText etSearch = (EditText) findViewById(R.id.etSearchText);
mSearchText = etSearch.getText().toString();

}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// return builder.create();
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
}

最佳答案

要从 Diloag 中查找 View ,您必须提供该 Dialog View 的引用。

 LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View mView = inflater.inflate(R.layout.dialog_save, null);
final EditText etSearch = (EditText)mView.findViewById(R.id.etSearchText);
builder.setView(mView)

完整代码:

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_search) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
// Get the layout inflater
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View mView = inflater.inflate(R.layout.dialog_save, null);
final EditText etSearch = (EditText)mView.findViewById(R.id.etSearchText);
builder.setView(mView)

// Add action buttons
.setPositiveButton("Search",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {

mSearchText = etSearch.getText().toString();

}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// return builder.create();
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}

关于android - 从 alertDialog 构建器获取 editText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153018/

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