2013-05-01 57 views
0

我已創建簡單的對話框,顯示當我上的按鈕 點擊每一件事工作正常 ,但我想的onkeydown不允許回去的onkeydown不允許回去當對話框啓動

這是我的活動類

public class MainActivity extends Activity implements OnClickListener { 

    private Button showdialog; 
    private Dialog dialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     showdialog=(Button)findViewById(R.id.showdialog); 
     showdialog.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View arg0) { 
     dialog = new Dialog(MainActivity.this, 
       android.R.style.Theme_Translucent); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setCancelable(true); 
     dialog.setContentView(R.layout.dialog); 
     dialog.show(); 
    } 
} 

,這是活動的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000424" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/showdialog" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="36dp" 
     android:text="showdialog" /> 

</RelativeLayout> 

,這是對話框XML

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="300dp" 
     android:layout_marginTop="200dp" 
     android:text="this is the dialog" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="100sp" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

</LinearLayout> 

回答

1

onKeyListener看看:

public class MainActivity extends Activity implements OnClickListener { 

    private Button showdialog; 
    private Dialog dialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     showdialog=(Button)findViewById(R.id.showdialog); 
     showdialog.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View arg0) { 
     dialog = new Dialog(MainActivity.this, 
       android.R.style.Theme_Translucent); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setCancelable(true); 
     dialog.setCanceledOnTouchOutside(false); 
     dialog.setContentView(R.layout.dialog); 
     dialog.setOnKeyListener(new Dialog.OnKeyListener() { 

     @Override 
     public boolean onKey(DialogInterface arg0, int keyCode, 
       KeyEvent event) { 
       if (keyCode == KeyEvent.KEYCODE_BACK) { 
        //DO NOTHING 
       } 
       return true; 
      } 
     }); 
     dialog.show(); 
    } 
} 
+0

事情是我不想要的onBackPressed()該類我想它的對話活動 – 2013-05-01 16:52:43

+0

,所以我不明白你所需要的。 – JackTurky 2013-05-01 16:55:45

+0

該對話框正在工作,但對話框啓動後,如果我點擊對開基本信息對話框 – 2013-05-01 16:59:50