gpt4 book ai didi

java - EditText getText 返回空

转载 作者:行者123 更新时间:2023-12-01 14:37:06 25 4
gpt4 key购买 nike

希望我只是传递一些愚蠢的东西。

问题:我的 EditText (2) getText() 在监听器中返回空字符串“”@运行时。请查看 onClick(...) 内的行

我怀疑这与我在生成器上 create() 之后膨胀对话框的 searchDialog 和 setContentView 的方式有关,但无法弄清楚。

代码:

private void initSearch() {
results = new ArrayList<Photo>();
AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
AlertDialog dialog = searchDialog.create();
LayoutInflater inflater = this.getLayoutInflater();
dialog.setContentView(R.layout.search_dialog);
searchDialog.setView(inflater.inflate(R.layout.search_dialog, null));
final EditText tagField = (EditText) dialog.findViewById(R.id.tagField);
final EditText valueField = (EditText) dialog.findViewById(R.id.valueField);
searchDialog.setTitle("Search Photos");
searchDialog.setMessage("Specify tag and value...");
searchDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
try { // PROBLEM: first two lines
String tag = tagField.getText().toString(); // TAG WILL BE EMPTY
String value = valueField.getText().toString(); // VALUE WILL BE EMPTY
String criteria = tag+":\""+value+"\"";
ArrayList<String> tags = new ArrayList<String>();
tags.add(criteria);
HomeScreen.results = c.getPhotosByTag(tags);

if(!tag.equalsIgnoreCase("person") || !tag.equalsIgnoreCase("location")){
throw new IllegalArgumentException("Tag types can only be location or person");
}else if(results.size() == 0) {
throw new IllegalArgumentException("No results");
}else {
Intent intent = new Intent(HomeScreen.this,SearchResults.class);
startActivity(intent);
}
} catch(Exception e) {
dialog.dismiss();
Utilities.createErrorDialog(HomeScreen.this, e.getMessage());
}

}
});
searchDialog.show();
}

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tagText"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/tag" />
<EditText
android:id="@+id/tagField"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<TextView
android:id="@+id/valueText"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/value" />
<EditText
android:id="@+id/valueField"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>

任何帮助将不胜感激。

最佳答案

试试这个代码->

LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.search_dialog, null);
final EditText tagField = (EditText) view.findViewById(R.id.tagField);
final EditText valueField = (EditText) view.findViewById(R.id.valueField);
searchDialog.setView(view);

关于java - EditText getText 返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16387160/

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