gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:37:45 28 4
gpt4 key购买 nike

我创建了简单的对话框,当我单击按钮时会显示该对话框一切正常但我希望 onKeydown 不允许返回

这是我的 Activity 课

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>

最佳答案

看看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();
}
}

关于java - 对话框启动时 onKeydown 不允许返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322060/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com