Android: 写一个 class 用于在任何地方使用警告对话框
Android: write a class for using alert dialog every where
我是 android 的新手,我尝试编写一个 class MyAlertDialog
用于警报对话框,以便在我需要警报对话框的任何地方使用它。我在 class 中写了一个方法 showAlertDialog
来做到这一点。我发现该方法必须是静态的。谁能告诉我为什么它应该是静态的?这是我的代码:
public class MyAlertDialog extends AppCompatActivity {
public static void alertDialogShow(Context context, String message) {
final Dialog dialog;
TextView txtAlertMsg;
TextView txtAlertOk;
dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_my_alert_dialog);
txtAlertOk = (TextView) dialog.findViewById(R.id.txtAalertOk);
txtAlertMsg = (TextView) dialog.findViewById(R.id.txtAlertMsg);
txtAlertMsg.setText(message);
txtAlertOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
我这样称呼它:
MyAlertDialog.alertDialogShow(MainActivity.this,"Here is my message");
不需要静态
你也可以使用非静态方式:
public class MyAlertDialog {
private Context context;
private String message;
public MyAlertDialog(Context context,String message){
this.context = context;
this.message = message;
}
public void alertDialogShow() {
final Dialog dialog;
TextView txtAlertMsg;
TextView txtAlertOk;
dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_my_alert_dialog);
txtAlertOk = (TextView) dialog.findViewById(R.id.txtAalertOk);
txtAlertMsg = (TextView) dialog.findViewById(R.id.txtAlertMsg);
txtAlertMsg.setText(message);
txtAlertOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
调用它:
MyAlertDialog myAlertDialog = new MyAlertDialog(MainActivity.this,"Here is my message");
myAlertDialog.alertDialogShow();
为什么它应该是静态的?
用于内存管理
怎么做?
将一个字段声明为静态意味着它只会存在一个实例
不属于具体实例,不能引用实例成员,即属于class本身
我是 android 的新手,我尝试编写一个 class MyAlertDialog
用于警报对话框,以便在我需要警报对话框的任何地方使用它。我在 class 中写了一个方法 showAlertDialog
来做到这一点。我发现该方法必须是静态的。谁能告诉我为什么它应该是静态的?这是我的代码:
public class MyAlertDialog extends AppCompatActivity {
public static void alertDialogShow(Context context, String message) {
final Dialog dialog;
TextView txtAlertMsg;
TextView txtAlertOk;
dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_my_alert_dialog);
txtAlertOk = (TextView) dialog.findViewById(R.id.txtAalertOk);
txtAlertMsg = (TextView) dialog.findViewById(R.id.txtAlertMsg);
txtAlertMsg.setText(message);
txtAlertOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
我这样称呼它:
MyAlertDialog.alertDialogShow(MainActivity.this,"Here is my message");
不需要静态
你也可以使用非静态方式:
public class MyAlertDialog {
private Context context;
private String message;
public MyAlertDialog(Context context,String message){
this.context = context;
this.message = message;
}
public void alertDialogShow() {
final Dialog dialog;
TextView txtAlertMsg;
TextView txtAlertOk;
dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_my_alert_dialog);
txtAlertOk = (TextView) dialog.findViewById(R.id.txtAalertOk);
txtAlertMsg = (TextView) dialog.findViewById(R.id.txtAlertMsg);
txtAlertMsg.setText(message);
txtAlertOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
调用它:
MyAlertDialog myAlertDialog = new MyAlertDialog(MainActivity.this,"Here is my message");
myAlertDialog.alertDialogShow();
为什么它应该是静态的?
用于内存管理
怎么做?
将一个字段声明为静态意味着它只会存在一个实例
不属于具体实例,不能引用实例成员,即属于class本身