2016-03-03 89 views
0

我試圖在AlertDialog類中創建一個登錄按鈕。我正在用按鈕膨脹自定義佈局。AlertDialog自定義佈局按鈕不可點擊

問題是按鈕什麼也不做。代碼編譯,沒有錯誤,只是不可點擊

這是我的課。

public class LoginDialogFragment extends DialogFragment { 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    //create inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    //set view with inflater 
    final View dialogView = inflater.inflate(R.layout.login_dialog, null); 
    builder.setView(inflater.inflate(R.layout.login_dialog, null)); 

    //set dialog to be non-cancelable 
    setCancelable(false); 

    final Dialog alertDialog = builder.create(); 

    //remove title bar 
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    //get editText fields to set font 
    EditText editUser = (EditText) dialogView.findViewById(R.id.user); 
    EditText editPass = (EditText) dialogView.findViewById(R.id.pass); 
    EditText editIp = (EditText) dialogView.findViewById(R.id.ip_addr); 
    Button login = (Button) dialogView.findViewById(R.id.login_button); 

    //get the font 
    Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/arial.ttf"); 

    //set the font 
    editIp.setTypeface(typeFace); 
    editPass.setTypeface(typeFace); 
    editUser.setTypeface(typeFace); 

    //set background and no dim affect 
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
    alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 

    //create onClick listener 
    login.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(getActivity(), "message", Toast.LENGTH_LONG).show(); 

     } 
    }); 

    // Create the AlertDialog object and return it 
    return alertDialog; 
} 

繼承人我的XML佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="15dp" 
    android:background="@drawable/dialog_bg" 
    android:backgroundDimEnabled="false"> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/imageView" 
     android:src="@drawable/optanix_logo" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:layout_alignParentTop="true" 
     android:padding="8dp" 
     android:layout_centerHorizontal="true" /> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:backgroundDimEnabled="false"> 

     <ImageView 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:src="@drawable/ic_action_user" 
      android:adjustViewBounds="true" 
      android:layout_marginTop="10dp" 
      android:padding="4dp" 
      android:background="@drawable/icon_bg" 
      android:layout_centerHorizontal="true" /> 

     <EditText 
      android:id="@+id/user" 
      android:layout_width="fill_parent" 
      android:layout_height="32dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edit_text_bg" 
      android:hint="Username" 
      android:textSize="10sp" 
      android:padding="4dp" /> 

    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:backgroundDimEnabled="false"> 

     <ImageView 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:src="@drawable/ic_action_lock_closed" 
      android:adjustViewBounds="true" 
      android:layout_marginTop="10dp" 
      android:padding="4dp" 
      android:background="@drawable/icon_bg" 
      android:layout_centerHorizontal="true" /> 

     <EditText 
      android:id="@+id/pass" 
      android:layout_width="fill_parent" 
      android:layout_height="32dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edit_text_bg" 
      android:hint="Password" 
      android:inputType="textPassword" 
      android:textSize="10sp" 
      android:padding="4dp" /> 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:backgroundDimEnabled="false"> 

     <ImageView 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:src="@drawable/ic_action_location" 
      android:adjustViewBounds="true" 
      android:layout_marginTop="10dp" 
      android:padding="4dp" 
      android:background="@drawable/icon_bg" 
      android:layout_centerHorizontal="true" /> 

     <EditText 
      android:id="@+id/ip_addr" 
      android:layout_width="fill_parent" 
      android:layout_height="32dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/edit_text_bg" 
      android:hint="IP address" 
      android:textSize="10sp" 
      android:padding="4dp" /> 

    </LinearLayout> 


    <Button 
     android:id="@+id/login_button" 
     android:layout_width="match_parent" 
     android:layout_height="32dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/login_button_bg" 
     android:textSize="10sp" 
     android:textAllCaps="false" 
     android:textColor="#FFFFFF" 
     android:text="@string/login_string"/> 

</LinearLayout> 
+0

你找到一個解決方案在這裏:在處理自定義對話框按鈕(http://stackoverflow.com/questions/4113939/handling-buttons-in-custom-dialogs/4114017#4114017) –

回答

0

這就是問題所在:

final View dialogView = inflater.inflate(R.layout.login_dialog, null); 
builder.setView(inflater.inflate(R.layout.login_dialog, null)); 

你兩次膨脹的觀點,而不是在對話框中點擊監聽器設置爲視圖。

做這樣的:

final View dialogView = inflater.inflate(R.layout.login_dialog, null); 
builder.setView(dialogView); 
+0

像魅力一樣工作,現在看起來如此明顯。但是,嘿,我是新來的。謝謝! – Dashby