2014-07-17 49 views
1

我需要在AlertDialog視圖中插入帶有子元素(如EditText)的自定義視圖。這很好,但是當用戶單擊AlertDialog ok按鈕時,我需要獲取子元素的信息,但不是獲取我在UI中插入的文本,而是獲取空字符串或空指針,如Log.e()中所示。在代碼中。在AlertDialog自定義視圖中膨脹並獲取輸入的信息

AlertDialog:

final AlertDialog.Builder alert = new AlertDialog.Builder(context); 
alert.setTitle("Title"); 

alert.setView(getLayoutInflater().inflate(R.layout.sonidovideo, null)); 
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 


    EditText a = (EditText) findViewById(R.id.segundos); 
    EditText b = (EditText) findViewById(R.id.segundosFin); 
    EditText c = (EditText) findViewById(R.id.segundosInicio); 
    Spinner d = (Spinner) findViewById(R.id.calidades); 
    Spinner e = (Spinner) findViewById(R.id.detecciones); 


    Log.e("Is null", (a==null) + ""); //False 
    Log.e("No text",(a.getText().toString().compareTo("")==0) + ""); //True 

    Log.e("Is null", (b==null) + ""); //True 

    Log.e("Is null", (c==null) + ""); //True 

    Log.e("Is null", (d==null) + ""); //True 

    Log.e("Is null", (e==null) + ""); //True 



    } 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

    dialog.cancel(); 
} 
}); 
alert.show(); 

R.layour.sonidovideo(sonidosvideo.xml):

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" > 

<EditText 
    android:id="@+id/segundosInicio" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/textView3" 
    android:ems="10" 
    android:inputType="numberDecimal" /> 

<EditText 
    android:id="@+id/segundos" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/textView2" 
    android:ems="10" 
    android:inputType="numberDecimal" > 
    <requestFocus /> 
</EditText> 



<Spinner 
    android:id="@+id/detecciones" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/textView4" 
    android:entries="@array/detecciones" /> 

<EditText 
    android:id="@+id/segundosFin" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/textView6" 
    android:ems="10" 
    android:inputType="numberDecimal" /> 

<Spinner 
    android:id="@+id/calidades" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/segundosFin" 
    android:entries="@array/calidades" /> 

對於獲得TE子項目我也試試這個,但結果是一樣的

EditText a = (EditText) getLayoutInflater().inflate(R.layout.sonidovideo, null).findViewById(R.id.segundos); 

任何幫助表示讚賞。

回答

0

試試這個

EditText a = (EditText) alert.findViewById(R.id.segundos); 
EditText b = (EditText) alert.findViewById(R.id.segundosFin); 
EditText c = (EditText) alert.findViewById(R.id.segundosInicio); 
Spinner d = (Spinner) alert.findViewById(R.id.calidades); 
Spinner e = (Spinner) alert. findViewById(R.id.detecciones); 

你既然是動態膨脹的佈局,你必須指定具有這些視圖的佈局。