school_guy school_guy - 5 months ago 18
Android Question

getText() in EditText is not working

I am currently trying to get a string out of an EditText field.

Here is my Code:

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();
}

}


This code is not working! According to the answers how to retrieve text from an EditText I did nothing wrong. The EditText is located in an AlertFragment.

The current output for the Log.d(...) is (and the same goes for the second):


apiKey Input:


Thx in advance.

Answer

Do not inflate a new view when the button is pressed.
Try using 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();
        }
    });