我目前正在尝试从EditText字段中获取字符串。
这是我的代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
public class FirstRunDialog extends DialogFragment {
public static final String DEBUG_TAG = FirstRunDialog.class.getSimpleName();
private static final String PREFS_NAME = "MyPrefsFile";
private LayoutInflater inflater;
private View dialogView;
private EditText input;
private DialogInterface.OnClickListener listener;
private SharedPreferences settings;
private SharedPreferences.Editor editor;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof DialogInterface.OnClickListener) {
listener = (DialogInterface.OnClickListener) activity;
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.app_name);
builder.setView(R.layout.first_run_dialog);
builder.setCancelable(false);
builder.setPositiveButton("Key-Testen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
dialogView = getActivity().getLayoutInflater().inflate(R.layout.first_run_dialog, null);
input = (EditText) dialogView.findViewById(R.id.editText);
editor = settings.edit();
editor.putString("apiKey", input.getText().toString());
editor.commit();
Log.d(DEBUG_TAG, "apiKey Input: " + input.getText().toString());
Log.d(DEBUG_TAG, "apiKey: " + settings.getString("apiKey", ""));
dialog.dismiss();
}
});
builder.setNegativeButton("Key-Erstellen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Open Browser, then go to: https://account.arena.net/applications
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://account.arena.net/applications"));
startActivity(browserIntent);
}
});
return builder.create();
}
}
此代码无效!根据答案如何从EditText中检索文本我没有做错任何事。 EditText位于AlertFragment中。
Log.d(...)的当前输出是(和第二个相同):
提前谢谢。apiKey输入:
答案 0 :(得分:1)
按下按钮时不要给新视图充气
尝试使用getDialog()
:
builder.setPositiveButton("Key-Testen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
input = (EditText) getDialog().findViewById(R.id.editText);
editor = settings.edit();
editor.putString("apiKey", input.getText().toString());
editor.commit();
Log.d(DEBUG_TAG, "apiKey Input: " + input.getText().toString());
Log.d(DEBUG_TAG, "apiKey: " + settings.getString("apiKey", ""));
dialog.dismiss();
}
});
答案 1 :(得分:0)
试试这个
从onclick中取出这一行
input = (EditText) dialogView.findViewById(R.id.editText);
并使视图膨胀
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
builder .setView(dialogView);
EditText input = (EditText) dialogView.findViewById(R.id.editText);
然后在onclick中输入.gettext()。tostring,对不起我的英文 我希望能帮助