10. Android框架和工具之 AppMsg(消息提示)

时间:2022-12-26 09:48:56

1. AppMsg

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

      10. Android框架和工具之 AppMsg(消息提示)

2. AppMsg使用:

(1)AppMsg下载地址:

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

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

10. Android框架和工具之 AppMsg(消息提示)

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

10. Android框架和工具之 AppMsg(消息提示)

10. Android框架和工具之 AppMsg(消息提示)

(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>

布局效果如下:

10. Android框架和工具之 AppMsg(消息提示)

前面提到可以显示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>

布局效果图,如下:

10. Android框架和工具之 AppMsg(消息提示)

(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(消息提示)

10. Android框架和工具之 AppMsg(消息提示)

10. Android框架和工具之 AppMsg(消息提示)

10. Android框架和工具之 AppMsg(消息提示)