对话框启动时,不允许onKeydown返回

时间:2013-05-01 16:19:57

标签: java android dialog 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();  
    } 
}

这是Activity 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 个答案:

答案 0 :(得分: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();  
    } 
}