1. AppMsg

  优雅的弹出类似Toast的消息提示,支持3种状态Alert(警告),Confirm(确认)以及Info(消息)。

      

2. AppMsg使用:

(1)AppMsg下载地址:

https://github.com/johnkil/Android-AppMsg

(2)下载成功之后,解压如下:

(3)导入library 和 sample 分别导入Eclipse如下:

(4)首先我们来到主布局文件activity_main.xml,如下:

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"> <LinearLayout
android:id="@+id/animated_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="48dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textAllCaps="true"
android:text="@string/style"
/>
<Spinner
android:id="@+id/style_spnr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:spinnerMode="dropdown"
android:entries="@array/styles"
android:prompt="@string/style"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textAllCaps="true"
android:text="@string/priority"
/>
<Spinner
android:id="@+id/priority_spnr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:spinnerMode="dropdown"
android:entries="@array/priorities"
android:prompt="@string/priority"
/> <CheckBox
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bottom" /> <LinearLayout
android:id="@+id/alt_parent"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:orientation="vertical" /> <CheckBox
android:id="@+id/parent_chk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/custom_parent" /> <EditText
android:id="@+id/provided_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/your_message_here"
/> <Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/show_appmsg" /> <Button
android:id="@+id/cancel_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/cancel_all" /> </LinearLayout>
</ScrollView>

布局效果如下:

前面提到可以显示Alert、Confirm、Info三种消息样式,其实还可以显示自定义的消息样式,这里我们自定义一个消息样式为sticky.xml,如下:

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

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"> <ImageButton
android:id="@+id/remove_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:minWidth="48dp"
android:minHeight="48dp"
android:src="@drawable/ic_action_cancel_inset"
style="@style/SelectableItem"/> <TextView
android:id="@android:id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/remove_btn"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="8dp"
android:textColor="#ff222222"
android:textIsSelectable="false"
android:textSize="14sp"
android:textStyle="bold"/> </RelativeLayout>

布局效果图,如下:

(5)在MainActivity工程之中找到主Activity,修改为extends Activity,如下:

 /*
* Copyright 2012 Evgeny Shishkin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package com.devspark.appmsg.sample; import android.app.Activity;
import android.animation.LayoutTransition;
import android.annotation.TargetApi;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner; import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import static android.os.Build.VERSION_CODES.JELLY_BEAN;
import static android.text.TextUtils.isEmpty;
import static android.view.Gravity.BOTTOM;
import static android.view.View.GONE;
import static android.view.View.VISIBLE; import com.devspark.appmsg.AppMsg; /**
* Sample of AppMsg library.
*
* @author hebao
*/
public class MainActivity extends Activity {
private static final int NORMAL_POSITION = 1;
private static final int INFO_POSITION = 2; private int mMsgCount;
private Spinner mStyle;
private Spinner mPriority;
private EditText mProvidedMsg;
private CheckBox mBottom;
private CheckBox mParent;
private ViewGroup mAltParent; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mProvidedMsg = (EditText) findViewById(R.id.provided_txt);
mStyle = (Spinner) findViewById(R.id.style_spnr);
mStyle.setSelection(INFO_POSITION);
mPriority = (Spinner) findViewById(R.id.priority_spnr);
mPriority.setSelection(NORMAL_POSITION);
mBottom = (CheckBox) findViewById(R.id.bottom);
mParent = (CheckBox) findViewById(R.id.parent_chk);
mAltParent = (ViewGroup) findViewById(R.id.alt_parent); mParent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mAltParent.setVisibility(isChecked ? VISIBLE : GONE);
mBottom.setVisibility(isChecked ? GONE : VISIBLE);
}
}); if (SDK_INT >= JELLY_BEAN) {
enableChangingTransition();
}
} /**
* 指使用该注解的方法适用于系统版本Android 4.1
* 注:Android 4.1的版本代号是Jelly Bean
*/
@TargetApi(JELLY_BEAN)
private void enableChangingTransition() {
ViewGroup animatedRoot = (ViewGroup) findViewById(R.id.animated_root);
animatedRoot.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
} /**
* Button onClick listener.
*
* @param v
*/
public void buttonClick(View v) {
switch (v.getId()) {
case R.id.show:
showAppMsg();
break;
case R.id.cancel_all:
AppMsg.cancelAll(this);
break;
default:
return;
}
} /**
* 显示App Message
*/
private void showAppMsg() {
mMsgCount++;
final int styleSelected = mStyle.getSelectedItemPosition();
final int priority = positionToPriority(mPriority.getSelectedItemPosition());
final CharSequence providedMsg = mProvidedMsg.getText();
final CharSequence msg = isEmpty(providedMsg)
? new StringBuilder().append(mStyle.getSelectedItem())
.append(" ").append(mPriority.getSelectedItem())
.append(" msg#").append(mMsgCount).toString()
: providedMsg;
final AppMsg.Style style;
boolean customAnimations = false;
AppMsg provided = null;
switch (styleSelected) {
case 0:
style = AppMsg.STYLE_ALERT;
break;
case 1:
style = AppMsg.STYLE_CONFIRM;
break;
case 3:
style = new AppMsg.Style(AppMsg.LENGTH_SHORT, R.color.custom);
customAnimations = true;
break;
case 4:
style = new AppMsg.Style(AppMsg.LENGTH_STICKY, R.color.sticky);
provided = AppMsg.makeText(this, msg, style, R.layout.sticky);
provided.getView()
.findViewById(R.id.remove_btn)
.setOnClickListener(new CancelAppMsg(provided));
break;
default:
style = AppMsg.STYLE_INFO;
break;
}
// create {@link AppMsg} with specify type
AppMsg appMsg = provided != null ? provided : AppMsg.makeText(this, msg, style);
appMsg.setPriority(priority);
if (mParent.isChecked()) {
appMsg.setParent(mAltParent);
} else {
if (mBottom.isChecked()) {
appMsg.setLayoutGravity(BOTTOM);
}
} if (customAnimations) {
appMsg.setAnimation(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
appMsg.show(); } private static int positionToPriority(int selectedItemPosition) {
switch (selectedItemPosition) {
case 0:
return AppMsg.PRIORITY_HIGH;
case 2:
return AppMsg.PRIORITY_LOW;
default:
return AppMsg.PRIORITY_NORMAL;
}
} @Override
protected void onPause() {
super.onPause();
// This is optional for 14+,
// also you may want to call it at your later convenience, e.g. onDestroy
if (SDK_INT < ICE_CREAM_SANDWICH) {
AppMsg.cancelAll(this);
}
} static class CancelAppMsg implements View.OnClickListener {
private final AppMsg mAppMsg; CancelAppMsg(AppMsg appMsg) {
mAppMsg = appMsg;
} @Override
public void onClick(View v) {
mAppMsg.cancel();
}
}
}

部署到模拟器上,如下:

10. Android框架和工具之 AppMsg(消息提示)的更多相关文章

  1. 7. Android框架和工具之 android-percent-support-lib-sample(百分比支持)

    1. android-percent-support-lib-sample介绍: 谷歌最新的百分比布局库的示例项目.其实LinearLayout的layout_weight也能实现百分比效果,不过这个 ...

  2. Android应用开发学习之Toast消息提示框

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来看Toast消息提示框的用法.使用Toast消息提示框一般有三个步骤: 1.  创建一个Toast对象.可 ...

  3. Android学习笔记通过Toast显示消息提示框

    显示消息提示框的步骤 这个很简单我就直接上代码了: Button show = (Button)findViewById(R.id.show); show.setOnClickListener(new ...

  4. 4. Android框架和工具之 android-async-http

    1. android-async-http   简介 主要有以下功能: (1)发送异步http请求,在匿名callback对象中处理response信息: (2)http请求发生在UI(主)线程之外的 ...

  5. 3. Android框架和工具之 xUtils(DbUtils )

    1. xUtils简介 xUtils 包含了很多实用的android工具.xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓 ...

  6. 3. Android框架和工具之 xUtils(HttpUtils)

    1. HttpUtils 作用: 支持同步,异步方式的请求: 支持大文件上传,上传大文件不会oom: 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD请求: 下载支持301/3 ...

  7. 13. Android框架和工具之 Android Drawable Factory

    1. AndroidDrawableFactory 一个生成Android应用所需尺寸图片的工具. 托管在Github之中: https://github.com/tizionario/Android ...

  8. 6. Android框架和工具之 JSON解析

    Android进阶笔记17:3种JSON解析工具(org.json.fastjson.gson)

  9. 3. Android框架和工具之 xUtils(BitmapUtils)

    1. BitmapUtils 作用: 加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象: 支持加载网络图片和本地图片: 内存管理使用 ...

随机推荐

  1. hdu 5310 Souvenir(BestCoder 1st Anniversary ($))

    http://acm.hdu.edu.cn/showproblem.php?pid=5310 题目大意:要买n个纪念品,可以单个买p元每个,可以成套买q元一套,每套有m个,求最少花费 #include ...

  2. Angular 中得 scope 作用域梳理

    $scope 的使用贯穿整个 Angular App 应用,它与数据模型相关联,同时也是表达式执行的上下文.有了 $scope 就在视图和控制器之间建立了一个通道,基于作用域视图在修改数据时会立刻更新 ...

  3. URAL 2069 Hard Rock (最短路)

    题意:给定 n + m 个街道,问你从左上角走到右下角的所有路的权值最小的中的最大的. 析:我们只要考虑几种情况就好了,先走行再走列和先走列再走行差不多.要么是先横着,再竖着,要么是先横再竖再横,要么 ...

  4. 1) data-options

    <select class="easyui-combobox" data-options="editable:false"> <select ...

  5. How Tomcat Works(七)

    本文接下来介绍并分析servlet容器,servlet容器是用来处理请求servlet资源,并为web客户端填充response对象的模块. servlet容器是org.apache.catalina ...

  6. AFNetWorking3.0使用 自签名证书的https请求

    前几日,项目组出于安全角度的考虑,要求项目中的请求使用https请求,因为是企业内部使用的app,因此使用了自签名的证书,而自签名的证书是不受信任的,所以我们就需要自己来做证书的验证,包括服务器验证客 ...

  7. (剑指Offer)面试题15:链表中倒数第k个结点

    题目: 输入一个链表,输出该链表中倒数第k个结点. 例如:链表中有6个结点,从头到尾依次为1,2,3,4,5,6,则该链表的倒数第3个结点为4. 链表结点定义: struct ListNode{ in ...

  8. JavaScript要点(十七) Math 对象

    来源:JavaScript 参考手册 Math 对象 Math 对象用于执行数学任务. Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(). 语法 var ...

  9. jquery validate使用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 关于Excel导入的HDR=YES; IMEX=1详解

    于Excel导入的HDR=YES; IMEX=1详解2011年12月27日 星期二 11:17 参数HDR的值:HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不 ...