我有一个活动,提示一个对话框片段。当对话框片段被关闭时,我想在父活动中调用一个方法。这是包含对话框片段的活动。
public class HomScr extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.das_boa);
initialize();
}
private void initialize(){
tv = findViewById(R.id.tv);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProEdiCon dia_fra = new ProEdiCon();
dia_fra.show((this).getSupportFragmentManager(), "pro_edi_con");
}
}
}
private void method_to_run_onDismiss(){
tv.setText("method to run is executed");
Toast.makeText(this, "method to run successfully executed on dismiss Dialog Fragment", Toast.LENGTH_SHORT).show();
}
}
下面的代码是DialogFragment,它在某些时候被关闭,然后父活动必须调用该方法以在关闭时运行。
public class ProEdiCon extends DialogFragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bun) {
View pro_vie = inflater.inflate(R.layout.pro_edi_dat, container, false);
TextView tv = pro_vie.findViewById(R.id.tv);
tv.setText("I am the Dialog Fragment who is gonna be dismissed soon");
Button btn = pro_vie.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
}
return pro_vie;
}
}
那么有人可以帮我吗?
答案 0 :(得分:1)
您可以使用Dialog
并设置“关闭监听器”,并在对话框关闭时监听事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProEdiCon dia_fra = new ProEdiCon();
dia_fra.show();
dia_fra.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//do some action here
}
});
}
}
,您的对话框将如下所示:
public class ProEdiCon extends Dialog {
public ProEdiCon (@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pro_edi_dat);
TextView tv = pro_vie.findViewById(R.id.tv);
tv.setText("I am the Dialog Fragment who is gonna be dismissed soon");
Button btn = pro_vie.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
答案 1 :(得分:1)
您必须创建这样的界面
CallBackListener.java
public interface CallBackListener {
void onDismiss();
}
然后在您的片段中
public class ProEdiCon extends DialogFragment {
private CallBackListener callBackListener;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activities
if (getActivity() instanceof CallBackListener)
callBackListener = (CallBackListener) getActivity();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bun) {
View pro_vie = inflater.inflate(R.layout.pro_edi_dat, container, false);
TextView tv = pro_vie.findViewById(R.id.tv);
tv.setText("I am the Dialog Fragment who is gonna be dismissed soon");
Button btn = pro_vie.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callBackListener != null)
callBackListener.onDismiss();
dismiss();
}
}
return pro_vie;
}
}
最后在您的活动中
public class HomScr extends AppCompatActivity implements CallBackListener {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.das_boa);
initialize();
}
private void initialize(){
tv = findViewById(R.id.tv);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProEdiCon dia_fra = new ProEdiCon();
dia_fra.show((this).getSupportFragmentManager(), "pro_edi_con");
}
}
}
private void method_to_run_onDismiss(){
tv.setText("method to run is executed");
Toast.makeText(this, "method to run successfully executed on dismiss Dialog Fragment", Toast.LENGTH_SHORT).show();
}
@Override
public void onDismiss() {
method_to_run_onDismiss();
}
}
答案 2 :(得分:0)
您可能想在Dialog类中使用DialogListener
接口,并在关闭对话框之前调用它。
与方法的接口
public interface MyInterface {
void method_to_run_onDismiss();
}
对话框-创建接口的实例并在dismiss();
public class ProEdiCon extends DialogFragment {
private MyInterface myInterface;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bun) {
...
myInterface = (MyInterface) context;
Button btn = pro_vie.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myInterface.method_to_run_onDismiss();
dismiss();
}
}
return pro_vie;
}
}
活动类实现接口并使用您已有的方法
Public class HomScr extends AppCompatActivity implements MyInterface {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.das_boa);
initialize();
}
private void initialize(){
tv = findViewById(R.id.tv);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProEdiCon dia_fra = new ProEdiCon();
dia_fra.show((this).getSupportFragmentManager(), "pro_edi_con");
}
}
}
private void method_to_run_onDismiss(){
tv.setText("method to run is executed");
Toast.makeText(this, "method to run successfully executed on dismiss Dialog Fragment", Toast.LENGTH_SHORT).show();
}
}