Get EditText value from a layout passed as parameter to an AlertDialog.Builder at the click of the button?

5

I use LayoutInflater in my view - View view = li.inflate(R.layout.alertdialog, null); I created an AlertDialog.Builder builder by setting the view, and the positive and negative buttons. My Layout has an EditText and I want to get the value of EditText after the click of the plus button. I use a variable of type EditText to retrieve the EditText reference of the Layout with the R.id.edtText, even so, using the log the value of it is null. How do I get the value of EditText in this case?

LayoutInflater li = getLayoutInflater();

    View view = li.inflate(R.layout.alertdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Criando novo arquivo de apresentação");
    builder.setView(view);

    builder.setPositiveButton("Criar",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    nomeArquivo = (EditText) findViewById(R.id.nomeArquivo);
                    String temp = diretorio + nomeArquivo.getText().toString() + ".cron";

This part of the code gives an error and terminates my application. - nomeArquivo.getText().toString()

xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/arquivoicon" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="38dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Nome do arquivo:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/nomeArquivo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:gravity="center"
    android:text="teste" >

    <requestFocus />
</EditText>

    
asked by anonymous 06.02.2014 / 23:22

1 answer

7

You will be using findViewById of Activity .

You must use findViewById of AlertDialog: dialog.findViewById(R.id.nomeArquivo);

LayoutInflater li = getLayoutInflater();

    View view = li.inflate(R.layout.alertdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Criando novo arquivo de apresentação");
    builder.setView(view);

    builder.setPositiveButton("Criar",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    nomeArquivo = (EditText) ((Dialog) dialog).findViewById(R.id.nomeArquivo);
                    String temp = diretorio + nomeArquivo.getText().toString() + ".cron";
    
06.02.2014 / 23:48