android 弹出窗口
1. xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
>
<Button Android:id="@+id/alert"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="Make an alert"></Button>
<Button Android:id="@+id/toast"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:text="Make a toast"></Button>
</LinearLayout>
2. 源文件
package a06.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class a06 extends Activity {
//定义变量
private Button alert = null;
private Button toast = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("郑文亮");
//根据Id得到控件对象
alert = (Button)findViewById(R.id.alert);
toast = (Button)findViewById(R.id.toast);
//给按钮设定单击事件监听器
alert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示AlertDialog
new AlertDialog.Builder(a06.this).setTitle("弹出例子").setMessage("您的信息").setNeutralButton("close01", new DialogInterface.OnClickListener() {
//点击AlertDialog上的按钮的事件处理代码
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("郑文亮");
}
}).show();
}
});
toast.setOnClickListener(new View.OnClickListener() {
//显示Toast
@Override
public void onClick(View v) {
Toast.makeText(a06.this, "<看到我了吗?一会我就消失了>", Toast.LENGTH_SHORT).show();
}
});
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述