【问题标题】:Showing Custom Dialog in Fragment Activity在片段活动中显示自定义对话框
【发布时间】:2015-06-07 05:40:18
【问题描述】:

我正在使用片段活动创建一个简单的应用程序。 它运行良好。现在,每次我用正在运行的应用程序按下手机的后退按钮时,它总是终止。 所以我决定添加一个自定义对话框,如果用户想要关闭应用程序,它将使用“YES”或“NO”按钮作为验证运行。

我的代码::

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

close_dialog.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>

ConfirmClose.java

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;

    }
}

现在,没什么错误,没什么问题,我认为它会起作用,但是当我按下手机的后退按钮和正在运行的应用程序时, 什么都没有发生,它只是像正常关闭一样关闭,自定义对话框没有显示。谁能帮我解决这个问题?

非常感谢!

注意:我正在使用我的旧安卓手机来测试应用程序。

【问题讨论】:

    标签: java android android-fragments android-dialogfragment android-dialog


    【解决方案1】:
                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,名称为 textDialogue
    【解决方案2】:
    1. 从 onBackPress 函数中删除 super.onBackPressed()

    2. 您是否尝试为确认对话框实现弹出窗口?为什么不直接使用alertDialogBuilder

      如果你想自定义一个弹窗片段,你应该使用DialogFragment而不是Fragment。

    3. 创建片段后,还需要显示,否则不会显示。

      我认为对于您的情况,您可以删除 CloseFragment 代码,并直接在 onBackPressed() 代码中构建一个 AlertDiablog。

    【讨论】:

      【解决方案3】:
      <?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>
      

      【讨论】:

        【解决方案4】:
        Fragment fragment =  new HomeFragment() ;
                final FragmentManager fragmentManager = getFragmentManager();           
                final Fragment f =fragmentManager.findFragmentById(R.id.content_frame);// your fragment insted of content_frame.......
        

        【讨论】:

          猜你喜欢
          • 2016-11-03
          • 1970-01-01
          • 2018-11-29
          • 1970-01-01
          • 2014-04-08
          • 1970-01-01
          • 2011-11-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多