Java 在片段活动中显示自定义对话框
我正在使用片段活动创建一个简单的应用程序。 它运转良好。现在,每当我在运行的应用程序中按下手机的后退按钮时,它总是终止。 因此,我决定添加一个自定义对话框,如果用户想要关闭应用程序,它将使用“是”或“否”按钮作为验证运行。Java 在片段活动中显示自定义对话框,java,android,android-fragments,android-dialogfragment,android-dialog,Java,Android,Android Fragments,Android Dialogfragment,Android Dialog,我正在使用片段活动创建一个简单的应用程序。 它运转良好。现在,每当我在运行的应用程序中按下手机的后退按钮时,它总是终止。 因此,我决定添加一个自定义对话框,如果用户想要关闭应用程序,它将使用“是”或“否”按钮作为验证运行。 我的代码:: MainActivity.java //NOTE: I want to call the custom dialog at the backpressed event @Override public void onBackPressed()
我的代码:: MainActivity.java
//NOTE: I want to call the custom dialog at the backpressed event
@Override
public void onBackPressed() {
super.onBackPressed();
Fragment fragment = null;
fragment = new ConfirmClose();
}
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ConfirmClose extends Fragment{
public ConfirmClose(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View promptView = inflater.inflate(R.layout.close_dialog, container);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity().getApplicationContext());
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {;
System.exit(0);
//this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
return promptView;
}
}
关闭对话框.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/confirmToPrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/confirm_to_close" <!-- Do you really want to close? --->
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
现在,没有错误,没有问题,我假设它会工作,但是,当我用运行中的应用程序按下手机的后退按钮时,
没有发生任何事情,只是像正常关闭一样关闭,没有显示自定义对话框。有人能帮我解决这个问题吗非常感谢
注意:我正在使用我的旧android手机测试应用程序
Fragment fragment = new HomeFragment() ;
final FragmentManager fragmentManager = getFragmentManager();
final Fragment f =fragmentManager.findFragmentById(R.id.content_frame);// your fragment insted of content_frame.......
super.onBackPressed()
alertDialogBuilder
如果您想定制弹出式片段,应该使用DialogFragment而不是fragment
请添加一些解释这是自定义对话框。。。并使用名称textDialogue创建如上所示的XML
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert_dialogue);
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Do You Want To Exit");
Toast.makeText(MainActivity.this, "Back Click", Toast.LENGTH_SHORT).show();
Button acceptButton = (Button) dialog.findViewById(R.id.acceptButton);
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
acceptButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
fragmentManager.beginTransaction().remove(f).commit();
finish();
}
});
dialog.show();
<?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:layout_gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/textDialog"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@drawable/square_btnshape"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="adfa"
android:textColor="#FFF"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/acceptButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/square_btnshape"
android:text=" Yes " />
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#FFFFFF" >
</TextView>
<Button
android:id="@+id/declineButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/square_btnshape"
android:text=" No " />
</LinearLayout>
</LinearLayout>