Android消息提示框Toast(1)

android:id=“@+id/customToast”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“完全自定义” >

<Button

android:id=“@+id/runToastFromOtherThread”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“其他线程” >

custom.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/toastLayout”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“vertical” >

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“1dp”

android:background=“#333399”

android:gravity=“center”

android:text=“ToastTitle”

android:textColor=“#ffffff” />

<LinearLayout

android:id=“@+id/toastContent”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginBottom=“1dp”

android:layout_marginLeft=“1dp”

android:layout_marginRight=“1dp”

android:background=“#3366ff”

android:orientation=“vertical”

android:padding=“15dp” >

<ImageView

android:id=“@+id/customImageToast”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center” />

<TextView

android:id=“@+id/customTextToast”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:gravity=“center”

android:paddingLeft=“10dp”

android:paddingRight=“10dp” />

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

private Button simpleToastBtn, simpleToastWithCustomPositionBtn,

imageToastBtn, customToastBtn, runToastFromOtherThreadBtn;

Handler handler = new Handler();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

simpleToastBtn = (Button) findViewById(R.id.simpleToast);

simpleToastWithCustomPositionBtn = (Button) findViewById(R.id.simpleToastWithCustomPosition);

imageToastBtn = (Button) findViewById(R.id.imageToast);

customToastBtn = (Button) findViewById(R.id.customToast);

runToastFromOtherThreadBtn = (Button) findViewById(R.id.runToastFromOtherThread);

simpleToastBtn.setOnClickListener(this);

simpleToastWithCustomPositionBtn.setOnClickListener(this);

imageToastBtn.setOnClickListener(this);

customToastBtn.setOnClickListener(this);

runToastFromOtherThreadBtn.setOnClickListener(this);

}

@Override

public void onClick(View v) {

Toast toast = null;

switch (v.getId()) {

case R.id.simpleToast:

Toast.makeText(MainActivity.this, “默认Toast样式”, Toast.LENGTH_LONG)

.show();

break;

case R.id.simpleToastWithCustomPosition:

toast = Toast.makeText(MainActivity.this, “自定义位置Toast”,

Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

break;

case R.id.imageToast:

toast = Toast.makeText(MainActivity.this, “带图片的Toast”,

Toast.LENGTH_LONG);

LinearLayout layout = (LinearLayout) toast.getView();

ImageView image = new ImageView(MainActivity.this);

image.setImageResource(R.drawable.ic_launcher);

layout.addView(image, 0);

toast.show();

break;

case R.id.customToast:

LayoutInflater inflater = getLayoutInflater();

View view = inflater.inflate(R.layout.custom, null);

ImageView customImage = (ImageView) view

.findViewById(R.id.customImageToast);

TextView customText = (TextView) view

.findViewById(R.id.customTextToast);

customImage.setImageResource(R.drawable.ic_launcher);

customText.setText(“完全自定义Toast”);

toast = new Toast(MainActivity.this);

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 30, 30);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(view);

toast.show();

break;

case R.id.runToastFromOtherThread:

new Thread(new Runnable() {

public void run() {

showToast();

}

}).start();

break;

}

}

public void showToast() {

handler.post(new Runnable() {

@Override

如何成为Android高级架构师!

架构师必须具备抽象思维和分析的能力,这是你进行系统分析和系统分解的基本素质。只有具备这样的能力,架构师才能看清系统的整体,掌控全局,这也是架构师大局观的形成基础。 你如何具备这种能力呢?一是来自于经验,二是来自于学习。

架构师不仅要具备在问题领域上的经验,也需要具备在软件工程领域内的经验。也就是说,架构师必须能够准确得理解需求,然后用软件工程的思想,把需求转化和分解成可用计算机语言实现的程度。经验的积累是需要一个时间过程的,这个过程谁也帮不了你,是需要你去经历的。

但是,如果你有意识地去培养,不断吸取前人的经验的话,还是可以缩短这个周期的。这也是我整理架构师进阶此系列的始动力之一。


成为Android架构师必备知识技能

对应导图的学习笔记(由阿里P8大牛手写,我负责整理成PDF笔记)

部分内容展示

《设计思想解读开源框架》

  • 目录
  • 热修复设计
  • 插件化框架设计

    《360°全方面性能优化》
  • 设计思想与代码质量优化
  • 程序性能优化

    《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
    **
    [外链图片转存中…(img-JUKg27Iv-1714555159720)]
    《360°全方面性能优化》
    [外链图片转存中…(img-y53hVi24-1714555159722)]
  • 设计思想与代码质量优化
    [外链图片转存中…(img-a3X8Ttji-1714555159724)]
  • 程序性能优化
    [外链图片转存中…(img-Q7majbqr-1714555159725)]
    《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
  • 26
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android Studio中,有两种常用的消息提示框可以使用:Toast提示框和AlertDialog。这两种提示框在功能和使用方式上有所不同。 1. Toast提示框是一种简单的消息提示框,用于向用户显示一条临时性的消息。它的代码示例如下: Toast.makeText(MainActivity.this, "有空输入!\n请重新输入!", Toast.LENGTH_SHORT).show(); 这段代码会在底部弹出一个短暂显示的提示框,其中包含有关输入错误的消息Toast提示框适用于简单的文本提示,对于输入错误等情况较为合适。 2. AlertDialog是一种更为复杂和功能丰富的提示框,可以用于实现多种功能,例如提示说明、单选框、复选框甚至登录功能。AlertDialog可以通过编写XML文件来实现更多的功能和更好的界面。下面是一个使用AlertDialog的示例代码: Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { //放在UI线程弹AlertDialog的代码 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示") .setMessage("有空输入!\n请重新输入!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //确定按钮的点击事件处理 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //取消按钮的点击事件处理 } }) .show(); } }); 这段代码会创建一个AlertDialog并在UI线程中显示出来。其中,通过AlertDialog.Builder来构建对话框的内容,可以设置标题、消息内容以及确定和取消按钮的点击事件。通过调用show()方法来显示对话框。 总结来说,Toast提示框适用于简单的文本提示,而AlertDialog则可以实现更多的功能和更好的界面。具体选择哪种消息提示框取决于实际需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值