DialogFragment和Interface的使用
程序员文章站
2024-02-17 19:25:22
...
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.
实现效果图:
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.弹框页面布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id_llyout_about_dialog"
android:layout_width="360dp"
android:layout_height="300dp"
android:background="@color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/id_tv_device_mac"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:maxLines="1"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/id_tv_device_version"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:maxLines="1"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/id_tv_soft_version"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:maxLines="1"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/id_tv_copyright"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:maxLines="2"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
<Button
android:id="@+id/id_b_about_ok"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@string/app_name"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>