0

我创建了简单的对话框,当我单击按钮时显示一切正常,但我希望 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>
4

1 回答 1

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();  
    } 
}
于 2013-05-01T16:47:29.270 回答