DialogFragment和Interface的使用

android.app.DialogFragment

官方文档,介绍了很多方法的使用:

void	dismiss()
Dismiss the fragment and its dialog.

void	dismissAllowingStateLoss()
Version of dismiss() that uses FragmentTransaction.commitAllowingStateLoss().

Dialog	getDialog()
boolean	getShowsDialog()
Return the current value of setShowsDialog(boolean).

int	getTheme()
boolean	isCancelable()
Return the current value of setCancelable(boolean).

void	onActivityCreated(Bundle savedInstanceState)
Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.

void	onAttach(Context context)
Called when a fragment is first attached to its context.

void	onCancel(DialogInterface dialog)
This method will be invoked when the dialog is canceled.

void	onCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment.

Dialog	onCreateDialog(Bundle savedInstanceState)
Override to build your own custom Dialog container.

void	onDestroyView()
Remove dialog.

void	onDetach()
Called when the fragment is no longer attached to its activity.

void	onDismiss(DialogInterface dialog)
This method will be invoked when the dialog is dismissed.

void	onSaveInstanceState(Bundle outState)
Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance of its process is restarted.

void	onStart()
Called when the Fragment is visible to the user.

void	onStop()
Called when the Fragment is no longer started.

void	setCancelable(boolean cancelable)
Control whether the shown Dialog is cancelable.

void	setShowsDialog(boolean showsDialog)
Controls whether this fragment should be shown in a dialog.

void	setStyle(int style, int theme)
Call to customize the basic appearance and behavior of the fragment's dialog.

int	show(FragmentTransaction transaction, String tag)
Display the dialog, adding the fragment using an existing transaction and then committing the transaction.

void	show(FragmentManager manager, String tag)
Display the dialog, adding the fragment to the given FragmentManager.

实现效果图:

DialogFragment和Interface的使用_第1张图片

 

1.主界面:

package com.example.admin.ztest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;


/**
 * Created by wrs on 2019/5/27,11:59
 * projectName: Ztest5
 * packageName: com.example.admin.ztest
 */
public class TwoActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        Button button4 = (Button) findViewById(R.id.button5);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AboutDialogFragment aboutDialog = new AboutDialogFragment();
                aboutDialog.show(getFragmentManager(), "AboutDialogFragment");
                aboutDialog.onSetClickDialogListener(new AboutDialogFragment.SetOnClickDialogListener() {
                    @Override
                    public void onClickDoalogListener(int type, boolean boolClick) {
                        Log.e("TAG", "return:" + type + "==" + boolClick);
                    }
                });

            }
        });
    }

    @Override
    public void onClick(View v) {
    }
}

2.DialogFragment:

package com.example.admin.ztest;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


/**
 * Created by wrs on 2019/5/27,12:02
 * projectName: Ztest5
 * packageName: com.example.admin.ztest
 * 就是如果你在创建AlertDialog的时候调用了setOnCancelListener
 * 这个mCancelMessage变量有作用,否则dismiss和cancel等同。
 * 其实cancel里面调用了dismiss
 *
 */
public class AboutDialogFragment extends DialogFragment implements DialogInterface.OnCancelListener,DialogInterface.OnDismissListener{
    private TextView TV_MCU_Version_Menu,TV_SoftVersion_Menu,TV_CopyRight_Menu,TV_DeviceMac;//TV_DeviceVersion
    private Button AboutSure;

    private SetOnClickDialogListener mSetOnClickListener;
    public void onSetClickDialogListener(SetOnClickDialogListener listener){
        this.mSetOnClickListener = listener;
    }
    //这个接口 如果是在项目中,请新建文件 统一管理
    public interface SetOnClickDialogListener {
        void onClickDoalogListener(int type,boolean boolClick);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chs_about_dialog,container,false);
        initView(view);
        return view;
    }

    private void initView(View V_AboutDialog) {
        AboutSure = (Button)V_AboutDialog.findViewById(R.id.id_b_about_ok);
        TV_MCU_Version_Menu = (TextView) V_AboutDialog.findViewById(R.id.id_tv_device_version);
        TV_SoftVersion_Menu = (TextView) V_AboutDialog.findViewById(R.id.id_tv_soft_version);
        TV_CopyRight_Menu = (TextView) V_AboutDialog.findViewById(R.id.id_tv_copyright);
        TV_DeviceMac = (TextView) V_AboutDialog.findViewById(R.id.id_tv_device_mac);
        TV_MCU_Version_Menu.setText(getResources().getString(R.string.app_name)+"MacCfg.DeviceVerString");
        TV_SoftVersion_Menu.setText(getResources().getString(R.string.app_name)+"MacCfg.App_versions");
        TV_CopyRight_Menu.setText("MacCfg.Copyright");
        TV_DeviceMac.setText(getResources().getString(R.string.app_name)+ "MacCfg.Mac");

        AboutSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().cancel();
                if (mSetOnClickListener!=null){
                    mSetOnClickListener.onClickDoalogListener(0,true);
                }
            }
        });

    }

    //    This method will be invoked when the dialog is canceled. 取消对话框时将调用此方法。
    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        Log.e("TAG", "onCancel:");
    }

    //This method will be invoked when the dialog is dismissed.
    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        Log.e("TAG", "onDismiss:");
    }


}

3.弹框页面布局:




    

        

        

        

        
    

    

 

你可能感兴趣的:(android,学习)