好的,所以我在自定义对话框中显示我的隐私策略,现在我想在用户单击对话框上的接受按钮时在MainActivity(调用此对话框的地方)中调用一个方法。我该怎么办?
我的ViewDialog类:
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ViewDialog {
public void showDialog(final Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
WebView webView = dialog.findViewById(R.id.webViewPolicy);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(activity, "Policy Accepted", Toast.LENGTH_SHORT).show();
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/policy.html");
dialog.show();
}
}
我从MainActivity拨打的电话:
ViewDialog alert = new ViewDialog();
alert.showDialog(this, "Privacy Policy");
答案 0 :(得分:1)
您需要像这样创建interface
:
public class ViewDialog {
public interface OnAction {
void accepted();
void declined();
}
private OnAction listener;
public ViewDialog(Context context) {
...
this.listener = (MyActivity)context;
...
}
}
并像下面这样在Activity
中使用它:
public class MyActivity extends ... implements ViewDialog.OnAction {
...
@Override
public void accepted() {
// execute action when use accepts;
}
@Override
public void declined() {
// execute action when use declines;
}
...
}
然后,您可以在OnAction
类中使用ViewDialog
侦听器,如下所示:
this.listener.accepted();
这将在您的accepted()
中调用Activity
方法并执行您在其中编写的代码。
PS 别忘了在初始化ViewDialog
实例时将其作为侦听器传递,如下所示:
ViewDialog alert = new ViewDialog(this);
祝你好运:)
答案 1 :(得分:1)
尝试以下操作:
1)MainActivity.class:-------------
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ViewDialog alert = new ViewDialog();
// Rather than passing an activity as argument, pass a WeakReference referring to this activity.
alert.showDialog(new WeakReference<MainActivity>(MainActivity.this), "Privacy Policy");
}
public void mainActivityMethodToCallWhenPolicyIsAccepted(){
Toast.makeText(getApplicationContext() , "Policy Accepted Main Activity Method" , Toast.LENGTH_LONG).show();
}
}
2)ViewDialog.class:-------
public class ViewDialog {
public void showDialog(WeakReference<MainActivity> weakReference, String msg){
if(weakReference != null) {
final MainActivity activity = weakReference.get();
if (activity != null) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
WebView webView = dialog.findViewById(R.id.webViewPolicy);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
//Toast.makeText(activity, "Policy Accepted", Toast.LENGTH_SHORT).show();
activity.mainActivityMethodToCallWhenPolicyIsAccepted();
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/policy.html");
dialog.show();
}
}
}
}