gpt4 book ai didi

android - 在 android 中使用界面和对话框

转载 作者:行者123 更新时间:2023-11-29 15:16:04 24 4
gpt4 key购买 nike

我在做什么::

  • 我在点击它时有一个按钮,我正在弹出一个对话框
  • UniversalDialog.java 有两个逻辑alertDialog.setNegativeButton & alertDialog.setPositiveButton

我想做什么::

  • 我想在里面添加实现代码(内层逻辑)alertDialog.setNegativeButton & alertDialog.setPositiveButton 在在 mainActivity 中定义的接口(interface)

  • 我怎样才能做到这一点


MainActivity.java(我的主要 Activity )

public class MainActivity extends Activity implements OnClickListener{

Button btn1;
UniversalDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button) findViewById(R.id.button1);
dialog = new UniversalDialog(MainActivity.this);

btn1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:

dialog.showAlert();

break;
}
}

UniversalDialog.java(我有对话框功能的类)

public class UniversalDialog  {

private final Context mContext;
private final String dialogTitle=null;
private final String dialogMessage=null;
private boolean dialogFlag=false;

public UniversalDialog(Context context) {
this.mContext = context;
}

public void showAlert(){
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

alertDialog.setTitle(dialogTitle);
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setMessage(dialogMessage);

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Go to home screen
alertDialog.setCancelable(true);
}
});

// On pressing Settings button
alertDialog.setPositiveButton("QuitApp", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
//Go to home screen
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(startMain);
}
});

alertDialog.show();
}
}

最佳答案

有几种可能:

1) 你可以传递给对象UniversalDialog MainActivity的构造函数(而不是只考虑上下文),并调用后者的方法

public UniversalDialog(MainActivity activity) {
...
}

2)你可以在MainActivity上实现一个特定的接口(interface)(你在其中指定要在MainActivity上调用的回调方法)并返回到UniversalDialog此项

public interface ActionInterface {
public void methodOk();
public void methodKO();
}

public class MainActivity ... implements ActionInterface {

@Override
protected void onCreate(Bundle savedInstanceState) {
...
dialog = new UniversalDialog(MainActivity.this, MainActivity.this);
...
}

public void methodOk() {
...
}

public void methodKO() {
...
}

}

public class UniversalDialog {
public UniversalDialog(Context context, ActionInterface action) {
...
}

public void showAlert(){
...

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
action.methodKO();
...
}
});

// On pressing Settings button
alertDialog.setPositiveButton("QuitApp", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
action.methodOk();
...
}
});

alertDialog.show();
}
}

关于android - 在 android 中使用界面和对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23854119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com