Android基础教程之简单的Button事件响应综合提示控件Toast的应用
2015-06-17 23:25
866 查看
大家好,Button按钮所触发的事件处理,我们称之为Event Handle,只不过在Android当中,按钮事件是由系统的Button.OnClickListener所控制,熟悉Java程序设计的读者对OnXxxListener应该不陌生.以下的Demo,我们将实现当点击Button时,TextView文字将发生改变,并在屏幕上出现一段时间的不同样式Toast提醒. 一、Toast作用 1.Toast是一种提供给用户简洁提示信息的视图。 2.该视图以浮于应用程序之上的形式呈现给用户。Toast提示界面不获取脚垫,所以不影响用户的操作。Toast提示就是在不影响用户使用呈现的同时,给用户提供某些提示信息。有两个例子就是音量控制和设置信息保存成功。 3.Android提供的Toast类可以创建和显示该Toast信息。 二、Toast常用方法 Toast.makeText(context, text, duration);//返回值为Toast Toast.setDuration(duration);//设置持续时间 Toast.setGravity(gravity, xOffset, yOffset);//设置toast位置 xOffset设置正值则向右偏移,设置负值则向左偏移 yOffset设置正值则向下偏移,设置负值则向上偏移 Toast.setText(s);//设置提示内容 Toast.show();//显示 Toast.getView()获取当前Toast的布局
关于Toast的小知识点就介绍到这里,接下来我会演示四种Toast表现样式,分别为普通的Toast,位置变化的Toast,带图片的Toast,自定的简单做法Toast。
先上效果图
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jp.logdemo.MainActivity" tools:ignore="MergeRootFrame" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/toast_common" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="普通Toast" android:textSize="15sp" /> <Button android:id="@+id/toast_seat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="位置偏移Toast" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/toast_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="带图片的Toast" android:textSize="15sp" /> <Button android:id="@+id/toast_div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="自定义的Toast" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
首先我们要获取布局文件中的id
// 分别普通Toast,位置偏移Toast,带图片的Toast,自定义的Toast Button toast_common, toast_seat, toast_image, toast_div; // 获取控件id private void initView() { // Toast toast_common = (Button) findViewById(R.id.toast_common); toast_seat = (Button) findViewById(R.id.toast_seat); toast_image = (Button) findViewById(R.id.toast_image); toast_div = (Button) findViewById(R.id.toast_div);}
设置点击事件:
// 创建点击事件 private void initEvent() { // Toast点击事件 toast_common.setOnClickListener(this); toast_seat.setOnClickListener(this); toast_image.setOnClickListener(this); toast_div.setOnClickListener(this);}
public void onClick(View v) { switch (v.getId()) { case R.id.toast_common: //普通的Toast sendCommonToast(); break; case R.id.toast_seat: //位置变化的Toast sendSeatToast(); break; case R.id.toast_image: //带图片的Toast sendImageToast(); break; case R.id.toast_div: //自定义的Toast sendDivToast(); break;}
做法如下:
/** * 一个普通的Toast * */ private void sendCommonToast() { Toast.makeText(this, "这是一个普通的Toast", Toast.LENGTH_LONG).show(); }
位置有变化的Toast
做法:
/** * 位置偏移的Toast */ private void sendSeatToast() { Toast toast = Toast.makeText(this, "这是一个改变位置的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 100, 50); //设置toast位置 //xOffset设置正值则向右偏移,设置负值则向左偏移 //yOffset设置正值则向下偏移,设置负值则向上偏移 toast.show(); }
带图片的Toast
/** * 带图片的Toast */ private void sendImageToast() { Toast toast = Toast.makeText(this, "这是一个带图片的Toast", Toast.LENGTH_LONG); // 获取一个可以添加图片的布局文件,获取当前Toast布局 LinearLayout layout_toast = (LinearLayout) toast.getView(); ImageView imageView = new ImageView(this); imageView.setBackgroundResource(R.drawable.ic_launcher); layout_toast.addView(imageView, 0); toast.show(); }
自定义Toast:
/** * 自定义Toast */ private void sendDivToast() { // LayoutInflater作用是将layout的xml布局文件实例化为View类对象。 LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.divtoast_main, null); Toast toast = new Toast(this); toast.setView(view); toast.show(); }
因为我也是个初学者,希望能帮到其他初学者~~我会持续更新的,可以相互关注的
相关文章推荐
- 极力推荐 自学视频java、php、python、android!
- android 倒计时
- Android四大组件之Activity
- QT For Android 修改中文名称
- Android GridView 实现九宫格效果(小恩爱首页效果)
- Android学习第一课:自定义日志LogCat输出
- Android中Adapter之SimpleCursorAdapter使用
- android学习笔记一 ---- android-sdk基础介绍
- Android开源项目PagerSlidingTabStrip
- android ctrl +鼠标左键直接打开xml文件
- Android中Cursor类的概念和用法
- Android使用百度定位API时获取的地址信息为null
- 使用 Fragment 实现底部导航
- Android 获取设备信息、获取手机信息
- 记录Android学习笔记和部分IOS收藏
- 菜鸟学Android笔记(三十七):cookies入门
- Android cpu 使用率控件(浮动窗口)附源码
- Android开机广播android.intent.action.BOOT_COMPLETED
- Android中的静态系统广播和动态系统广播
- android模拟器访问localhost或127.0.0.1报错