gpt4 book ai didi

java - 我无法从 DialogFragment 中的 EditText 对象中获取文本(获取空字符串,即 "")

转载 作者:行者123 更新时间:2023-12-01 13:02:28 25 4
gpt4 key购买 nike

我正在扩展 DialogFragment,但在检索通过 DialogFragment UI 输入的文本时遇到问题。由于某种原因,我收到一个空字符串(“”)。

也许:1)。它没有通过以下方式获取正确的 View 查看 v = inflater.inflate(R.layout.add_dialog, null);2)。它没有抓取正确的 EditText3)。我需要通过 DialogInterface 对象使用一些东西 通过param传入

谢谢你,彼得

代码......

public class AddDialog extends DialogFragment {

EditText editText;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final foodhouseDatabaseAdapter myDBAdapter;
myDBAdapter = new foodhouseDatabaseAdapter(this.getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();

builder.setMessage("Please enter the item to be added");
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 'Close dialog box' ? .dismiss() ???
}
});

builder.setNegativeButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {


View v = inflater.inflate(R.layout.add_dialog, null);

/**
* this line is setting 'editText' to an empty string
*/
editText = (EditText) v.findViewById(R.id.user_entered_item_name);

String x = editText.getText().toString();

if (!x.isEmpty()) {

// Capitalize first letter of user entered string
String item = capitalizeFirstLetter(x);

long ID = myDBAdapter.insertData(1, item);

if (ID < 0) {
Message.message(getActivity(), "Item was not added");
} else {
Message.message(getActivity(), item + " added");
editText.setText("");
}
} else {
Message.message(getActivity(), "We did not see anything to insert");
}

}
});
builder.setView(inflater.inflate(R.layout.add_dialog, null));

return builder.create();
}


/** http://stackoverflow.com/questions/5725892/
* how-to-capitalize-the-first-letter-of-word-in-a-string-using-java */
public String capitalizeFirstLetter(String original){
if(original.length() == 0)
return original;
return original.substring(0, 1).toUpperCase(Locale.US) + original.substring(1);
}

}

最佳答案

您使用充气机的方式是错误的。使用getSystemService

示例:

LayoutInflater inflater = (LayoutInflater) getActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

关于java - 我无法从 DialogFragment 中的 EditText 对象中获取文本(获取空字符串,即 ""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23418553/

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