2013-08-27 131 views
0

我對話片段中實現這樣的隱藏按鈕,Android的

public class SessionExpiredFragment extends DialogFragment { 

    public interface SessionExpiredFragmentListener { 
     public void onCancelLoginProcessPressed(DialogFragment dialog); 

     // validValues = true if fields are not empty and email is a valid 
     // email, 
     // else validValues = false; 
     public void onOKLoginProcessPressed(DialogFragment dialog, 
       boolean validValues); 
    } 

    SessionExpiredFragmentListener mListener; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (SessionExpiredFragmentListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement SessionExpiredFragmentListener"); 
     } 
    } 

    // UI references 
    private EditText mEmailView; 
    private EditText mPasswordView; 
    private View mLoginFormView; 
    private View mLoginStatusView; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     View view = inflater.inflate(R.layout.login_fragment_layout, null); 
     /* 
     * Get edit texts references 
     */ 
     mEmailView = (EditText) view.findViewById(R.id.email); 
     mPasswordView = (EditText) view.findViewById(R.id.password); 
     mLoginFormView = view.findViewById(R.id.login_form); 
     mLoginStatusView = view.findViewById(R.id.login_status); 

     /* 
     * Set builder values 
     */ 
     builder.setMessage(R.string.session_expired_title) 
       .setView(view) 
       .setPositiveButton(R.string.action_ok, 
         new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, 
            int which) { 
           boolean validValues = true; 
           String email = mEmailView.getText().toString(); 
           String password = mPasswordView.getText() 
             .toString(); 
           if (TextUtils.isEmpty(email) 
             || TextUtils.isEmpty(password)) 
            validValues = false; 
           if (!isValidEmail(email)) 
            validValues = false; 

           mListener.onOKLoginProcessPressed(
             SessionExpiredFragment.this, 
             validValues); 
          } 
         }) 
       .setNegativeButton(R.string.action_cancel, 
         new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, 
            int which) { 
           mListener 
             .onCancelLoginProcessPressed(SessionExpiredFragment.this); 

          } 
         }); 
     return builder.create(); 
    } 

,它有正負按鈕。我需要做的是當按下「正」按鈕時隱藏它們。我使用這個Listener,所以我可以在我的活動中聽,但那也沒有幫助我。 HOW(和WHERE添加該代碼)隱藏按鈕?謝謝您的幫助。

+0

請參閱此處:http://stackoverflow.com/questions/4127725/how-can-i-remove-a-button-or-make- it-in-in-android – user2143600

+0

如何幫助我使用不屬於佈局的按鈕 - 使用.setPositiveButton和.setNegativeButton添加按鈕。如果我使用findById查找它們,他們的ID是什麼?我應該在哪個視圖中調用findById? – Mediha

回答

0
Button negativeButton = alert.getButton(AlertDialog.BUTTON_NEGATIVE); 
buttonNo.setEnabled(false); 
+0

我會將此標記爲答案,但這需要在onStart()方法中完成,否則按鈕將爲空。 – Mediha

+0

我們如何從'SessionExpiredFragment'的實例獲取'alert'? –

0

那麼你應該考慮創建自定義視圖,並通過代碼

管理按鈕的知名度
0

只是覆蓋在onStart()在DialogFragment,並保持到按鈕的引用。您可以稍後使用該參考重新啓用按鈕:

Button positiveButton; 

@Override 
public void onStart() { 
    super.onStart(); 
    AlertDialog d = (AlertDialog) getDialog(); 
    if (d != null) { 
     positiveButton = (Button)d.getButton(Dialog.BUTTON_POSITIVE); 
     positiveButton.setEnabled(false); 
    } 

}