- 添加公用单元文件com..java
package com.example.myapplication;
import android.app.AlertDialog;
import android.content.Context;
import android.widget.Toast;
public class com {
//提示信息
public static void ShowMsg(String msg,Context context) {
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setTitle("提示");
dlg.setMessage(msg);
dlg.setPositiveButton("确定",null);
dlg.show();
}
//闪现提示
public static void DisplayToast(String msg,Context context) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
3.MainActivity.java新建方法名btn_clicked并调用com.java的方法
public void btn_clicked(View view){
com.ShowMsg("这是提示框",MainActivity.this);
com.DisplayToast("这是闪现提示",getBaseContext());
}
4.运行效果