2015-05-04 50 views
1

我需要使用方法onWindowFocusChange()來關閉AlertDialog中的系統對話框,所以我決定擴展AlertDialog並實現該方法。如何在對話框中實現onWindowFocusChange()方法(Android)

public class MyAlertDialog extends AlertDialog {  
    private Context context; 

    protected MyAlertDialog(Context context) { 
     super(context); 
     this.context = context; 
    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 

     if (!hasFocus) { 
      Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
      context.sendBroadcast(closeDialogs); 
     } 
    } 
} 

當我打電話從AlertDialog.Buildercreate()show(),這些方法得到的回覆是不執行的AlertDialog但不是MyAlertDialog對象和onWindowsFocusChanged()。顯然我不能投AlertDialogMyAlertDialog

AlertDialog dialog = new MyAlertDialog(this); 

MyAlertDialog.Builder builder = new MyAlertDialog.Builder(this); 
builder.setMessage(...); 
builder.setCancelable(false); 
builder.setNeutralButton(...) 
builder.show(); // Returns AlertDialog 
// dialog = builder.show(); -> dialog doesn't execute onWindowsFocusChanged() 
// dialog = (MyAlertDialog) builder.show() -> Not allowed (ClassCastException) 

所以,我怎麼能創建並顯示一個MyAlertDialog或另一種方式來關閉系統對話框時,Dialog是顯示?我查找了信息,但是我沒有找到任何信息。

在此先感謝。

回答

1

要使用onWindowFocusChanged,您必須在xml中創建一個帶有2個按鈕的Dialog佈局,創建一個Dialog類並擴展Dialog,並使用它設置Dialog的內容視圖。

XML對話框(myDialog.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="280dp" 
     android:layout_height="50dp" 
     android:layout_marginBottom="20dp" 
     android:layout_marginLeft="24dp" 
     android:layout_marginRight="24dp" 
     android:layout_marginTop="24dp" 
     android:text="Title" 
     android:textColor="@color/black" 
     android:textSize="22sp" /> 

    <EditText 
     android:id="@+id/password" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_marginBottom="24dp" 
     android:layout_marginLeft="24dp" 
     android:layout_marginRight="24dp" 
     android:hint="Password" 
     android:inputType="textPassword" 
     android:textSize="22sp" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="52dp" 
     android:gravity="end"> 

     <Button 
      android:id="@+id/btn_no" 
      android:layout_width="wrap_content" 
      android:layout_height="36dp" 
      android:layout_marginBottom="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="8dp" 
      android:background="@android:color/white" 
      android:clickable="true" 
      android:gravity="center" 
      android:maxWidth="128dp" 
      android:text="Cancel" 
      android:textColor="#5DBCD2" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/btn_yes" 
      android:layout_width="wrap_content" 
      android:layout_height="36dp" 
      android:layout_marginBottom="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="8dp" 
      android:background="@android:color/white" 
      android:clickable="true" 
      android:gravity="center" 
      android:maxWidth="128dp" 
      android:text="Login" 
      android:textColor="#5DBCD2" 
      android:textStyle="bold" /> 

    </LinearLayout> 
</LinearLayout> 

Dialog類:

public class MyDialog extends Dialog { 
    private Context context; 

    public F50Dialog(Context context) { 
     super(context); 
     this.context = context; 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
    } 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     if (!hasFocus) { 
      Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
      sendBroadcast(closeDialogs); 
     } 
    } 
} 

創建您在活動對話框:

private void myDialog() { 

     LayoutInflater li = LayoutInflater.from(this); 
     View promptsView = li.inflate(R.layout.myDialog, null, false); 

     final MyDialog alert = new MyDialog(this); 
     final EditText input = (EditText) promptsView.findViewById(R.id.password); 

     spin.setAdapter(mOperatorsAryAdapter); 

     alert.setContentView(promptsView); 
     alert.setCancelable(false); 
     alert.setTitle("Password Required"); 
     alert.findViewById(R.id.btn_no).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       alert.dismiss(); 
      } 
     }); 
     alert.findViewById(R.id.btn_yes).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (???) { 
        alert.dismiss(); 
       } else { 

       } 
      } 
     }); 
     alert.show(); 
    } 

現在onWindowFocusChanged必火!

+0

謝謝您的回答!它回答我的問題,它應該工作。但是,我告訴你我是如何解決它的:在'onWindowFocusChanged()'方法中,當活動失去焦點(顯示一個對話框,一個包含操作欄中顯示的子菜單的菜單等)時,我將啓動一個服務每300毫秒運行一次的線程關閉系統對話框。當活動在'onWindowFocusChanged()'方法中再次獲得焦點時,服務(和線程)將停止。因此,系統對話從不顯示。 – jelogar

0

嘗試將覆蓋方法:

@Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     if (!hasFocus) { 
      Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
      sendBroadcast(closeDialogs); 
     } 
    } 

到了活動本身。

並使用AlertDialog類。每當出現系統對話框時,它將通過調用活動內部發生的方法來關閉,不需要從AlertDialog類繼承。

+0

我已經在活動內部有'onWindowsFocusChanged()',但是當顯示一個'Dialog'時,它需要焦點並且活動會丟失它。所以有必要在'Dialog'內再次實現'onWindowsFocusChanged()',以在顯示時保持關閉系統對話框。 – jelogar

相關問題