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. 野火STM32 Flash&sd卡模拟U盘

    在USB库文件mass_mal.c中添加对flash和sd读写的函数,USB库调用这些函数从而实现模拟U盘的功能 //mass_mal.c /* Includes ------------------ ...

  2. unigui 导入导出数据

    导入:首先要用TUniFileUpload将文件从客户端上传至服务端,然后完成导入. TUniFileUpload上传文件的演示代码: UniFileUpload1.Execute; UniFileU ...

  3. LeetCode258:Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  4. mysql show processlist 命令详解

    命令格式 SHOW [FULL] PROCESSLIST SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPE ...

  5. css之clear属性

    clear属性用来设置元素左右两边是否可以存在浮动元素. 它的值包括:left,right,both,none.其中none代表左右两边可以出现浮动元素.

  6. Java中的BASE64

    located in rt.jar... public class sun.misc.BASE64Encoder extends sun.misc.CharacterEncoder{ //.. } p ...

  7. effective c++ (二)

    条款04:确定对象使用前已先被初始化 1.由于 c part of c++而且初始化可能导致运行期成本,那么就不保证发生初始化:例如arry是c part of c++的部分从而不能保证初始化,而ST ...

  8. 利用HTML5开发Android(4)---HTML5本地存储之Web Storage

    Web Storage是HTML5引入的一个非常重要的功能,可以在客户端本地存储数据,类似HTML4的cookie,但可实现功能要比cookie强大的多,cookie大小被限制在4KB,Web Sto ...

  9. 如何在KVM中管理存储池

    来自:http://blog.csdn.net/my2005lb/article/details/8635661 KVM平台以存储池的形式对存储进行统一管理,所谓存储池可以理解为本地目录.通过远端磁盘 ...

  10. redis-在乌班图下设置自动启动

    一.修改redis.conf 1.打开后台运行选项,默认情况下,Redis不在后台运行: daemonize yes 2.配置log文件地址,默认使用标准输入,即打印在命令行终端 的窗口上 logfi ...