第二步:在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
-
Description:
-
date: 2021/7/12 15:47
-
@author yiqi
-
@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
-
Description:
-
date: 2021/7/12 15:47
-
@author yiqi
-
@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;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
推荐学习资料
- 脑图
[外链图片转存中…(img-1Kly9IgJ-1711745869873)]
[外链图片转存中…(img-Vv5JJNJw-1711745869873)]
[外链图片转存中…(img-x50L0Hz2-1711745869873)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-CFVnb2kp-1711745869874)]
推荐学习资料
- 脑图
[外链图片转存中…(img-0UpHR7d1-1711745869874)]
[外链图片转存中…(img-UzeFb7vK-1711745869874)]
[外链图片转存中…(img-X4BsvM7u-1711745869874)]