DialogFragment的传值问题
Posted 我想月薪过万
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DialogFragment的传值问题相关的知识,希望对你有一定的参考价值。
继 DialogFragment的简单使用 之后,我们来聊聊 DialogFragment的传值问题。
效果展示:
Activity 向 DialogFragment 传值
方法一:使用 newInstance() + Bundle + setArguments()
第一步:完善 MyDialogFragment 代码
package com.wust.mydialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import org.w3c.dom.Text;
/**
* ClassName: MyDialogFragment <br/>
* Description: <br/>
* date: 2021/7/12 15:47<br/>
*
* @author yiqi<br />
* @QQ 1820762465
*/
public class MyDialogFragment extends DialogFragment {
private View mRootView;
private Button btn_close;
private stateListener mStateListener;
private String mStr;
private TextView tv_title;
//这个是 Activity 向 DialogFragment 传值的关键代码段
public static MyDialogFragment newInstance(String str) {
MyDialogFragment myDialogFragment = new MyDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title",str);
myDialogFragment.setArguments(bundle);
return myDialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置样式
setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null){
//获取布局
mRootView = getLayoutInflater().inflate(R.layout.dialog_fragment,container,false);
}
btn_close = mRootView.findViewById(R.id.btn_close);
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用回调
mStateListener.close();
}
});
tv_title = mRootView.findViewById(R.id.tv_title);
//设置传过来的值
Bundle arguments = this.getArguments();
tv_title.setText(arguments.getString("title"));
return mRootView;
}
@Override
public void onStart() {
//以下代码必须在 onStart() 方法中才有效
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 500;
window.setAttributes(params);
super.onStart();
}
//创建回调,监听dialog的关闭
public interface stateListener{
void close();
}
//暴露接口
public void setStateListener(stateListener listener){
this.mStateListener = listener;
}
}
第二步:在Activity中传值
package com.wust.mydialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private TextView tv_text;
private EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = findViewById(R.id.tv_text);
et_text = findViewById(R.id.et_text);
}
public void showDialog(View v){
//这句代码是传值关键
final MyDialogFragment myDialogFragment = MyDialogFragment.newInstance(et_text.getText().toString());
myDialogFragment.show(getSupportFragmentManager(),"123456");
myDialogFragment.setStateListener(new MyDialogFragment.stateListener() {
@Override
public void close() {
myDialogFragment.dismiss();
}
});
}
}
方法二:使用 构造方法 传值
第一步:完善 MyDialogFragment 代码
package com.wust.mydialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import org.w3c.dom.Text;
/**
* ClassName: MyDialogFragment <br/>
* Description: <br/>
* date: 2021/7/12 15:47<br/>
*
* @author yiqi<br />
* @QQ 1820762465
*/
public class MyDialogFragment extends DialogFragment {
private View mRootView;
private Button btn_close;
private stateListener mStateListener;
private String mStr;
private TextView tv_title;
//这个是 Activity 向 DialogFragment 传值的关键代码段
public MyDialogFragment(String str) {
this.mStr = str;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置样式
setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null){
//获取布局
mRootView = getLayoutInflater().inflate(R.layout.dialog_fragment,container,false);
}
btn_close = mRootView.findViewById(R.id.btn_close);
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用回调
mStateListener.close();
}
});
tv_title = mRootView.findViewById(R.id.tv_title);
tv_title.setText(mStr);
return mRootView;
}
@Override
public void onStart() {
//以下代码必须在 onStart() 方法中才有效
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 500;
window.setAttributes(params);
super.onStart();
}
//创建回调,监听dialog的关闭
public interface stateListener{
void close();
}
//暴露接口
public void setStateListener(stateListener listener){
this.mStateListener = listener;
}
}
第二步:在Activity中传值
package com.wust.mydialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private TextView tv_text;
private EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = findViewById(R.id.tv_text);
et_text = findViewById(R.id.et_text);
}
public void showDialog(View v){
//这句代码是传值关键
final MyDialogFragment myDialogFragment = new MyDialogFragment(et_text.getText().toString());
myDialogFragment.show(getSupportFragmentManager(),"123456");
myDialogFragment.setStateListener(new MyDialogFragment.stateListener() {
@Override
public void close() {
myDialogFragment.dismiss();
}
});
}
}
方法三:使用 public方法 传值
第一步:完善 MyDialogFragment 代码
package com.wust.mydialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import org.w3c.dom.Text;
/**
* ClassName: MyDialogFragment <br/>
* Description: <br/>
* date: 2021/7/12 15:47<br/>
*
* @author yiqi<br />
* @QQ 1820762465
*/
public class MyDialogFragment extends DialogFragment {
private View mRootView;
private Button btn_close;
private stateListener mStateListener;
private String mStr;
private TextView tv_title;
//这个是 Activity 向 DialogFragment 传值的关键代码段
public void setTitle(String str) {
this.mStr = str;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置样式
setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null){
//获取布局
mRootView = getLayoutInflater().inflate(R.layout.dialog_fragment,container,false);
}
btn_close = mRootView.findViewById(R.id.btn_close);
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用回调
mStateListener.close();
}
});
tv_title = mRootView.findViewById(R.id.tv_title);
tv_title.setText(mStr);
return mRootView;
}
@Override
public void onStart() {
//以下代码必须在 onStart() 方法中才有效
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 500;
window.setAttributes(params);
super.onStart();
}
//创建回调,监听dialog的关闭
public interface stateListener{
void close();
}
//暴露接口
public void setStateListener(stateListener listener){
this.mStateListener = listener;
}
}
第二步:在Activity中传值
package com.wust.mydialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private TextView tv_text;
private EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = findViewById(R.id.tv_text);
et_text = findViewById(R.id.et_text);
}
public void showDialog(View v){
final MyDialogFragment myDialogFragment = new MyDialogFragment();
//这句代码是传值关键
myDialogFragment.setTitle(et_text.getText().toString());
myDialogFragment.show(getSupportFragmentManager(),"123456");
myDialogFragment.setStateListener(new MyDialogFragment.stateListener() {
@Override
public void close() {
myDialogFragment.dismiss();
}
});
}
}
DialogFragment 向 Activity 传值
方法:回调
这里我们就借用 DialogFragment的简单使用 中的 close() 回调方法用一下,将我们刚才设置给 DialogFragment 的值 传给 Activity。
第一步:完善 MyDialogFragment 代码
package com.wust.mydialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import org.w3c.dom.Text;
/**
* ClassName: MyDialogFragment <br/>
* Description: <br/>
* date: 2021/7/12 15:47<br/>
*
* @author yiqi<br />
* @QQ 1820762465
*/
public class MyDialogFragment extends DialogFragment {
private View mRootView;
private Button btn_close;
private stateListener mStateListener;
private String mStr;
private TextView tv_title;
private TextView tv_title1;
//这个是 Activity 向 DialogFragment 传值的关键代码段
public void setTitle(String str) {
this.mStr = str;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置样式
setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mRootView == null){
//获取布局
mRootView = getLayoutInflater().inflate(R.layout.dialog_fragment,container,false);
tv_title1 = mRootView.findViewById(R.id.tv_title);
}
btn_close = mRootView.findViewById(R.id.btn_close);
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用回调 传值
mStateListener.close(tv_title1.getText().toString());
}
});
tv_title = mRootView.findViewById(R.id.tv_title);
tv_title.setText(mStr);
return mRootView;
}
@Override
public void onStart() {
//以下代码必须在 onStart() 方法中才有效
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 500;
window.setAttributes(params);
super.onStart();
}
//创建回调,监听dialog的关闭
public interface stateListener{
void close(String str);
}
//暴露接口
public void setStateListener(stateListener listener){
this.mStateListener = listener;
}
}
第二步:在Activity中接收值
package com.wust.mydialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private TextView tv_text;
private EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_text = findViewById(R.id.tv_text);
et_text = findViewById(R.id.et_text);
}
public void showDialog(View v){
final MyDialogFragment myDialogFragment = new MyDialogFragment();
//这句代码是传值关键
myDialogFragment.setTitle(et_text.getText().toString());
myDialogFragment.show(getSupportFragmentManager(),"123456");
myDialogFragment.setStateListener(new MyDialogFragment.stateListener() {
@Override
public void close(String str) {
//接收值的关键
tv_text.setText(str);
myDialogFragment.dismiss();
}
});
}
}
效果展示:
以上是关于DialogFragment的传值问题的主要内容,如果未能解决你的问题,请参考以下文章