import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView; import java.io.InputStream; /**
* UI工具类
* @version 1.0
*/
public class UiUtilities {
/**
* 设置view的显示状态
*/
public static void setVisibilitySafe(View view, int visibility) {
if (view != null && view.getVisibility() != visibility) {
view.setVisibility(visibility);
}
} /**
* 设置view的显示状态
*/
public static void setVisibilitySafe(View parent, int id, int visibility) {
if (parent != null) {
setVisibilitySafe(parent.findViewById(id), visibility);
}
} public static void setPressedSafe(View view, boolean pressed) {
if (view != null && view.isPressed() != pressed) {
view.setPressed(pressed);
}
} public static void setEnabledSafe(View parent, int id, boolean enabled) {
if (parent != null) {
View view = parent.findViewById(id);
if (view != null) {
view.setEnabled(enabled);
}
}
} public static void setOnClickListenerSafe(View parent, int id, OnClickListener l) {
if (parent != null) {
View view = parent.findViewById(id);
if (view != null) {
view.setOnClickListener(l);
}
}
} public static void requestFocus(View view) {
if (view != null) {
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
}
} public static boolean isEditTextEmpty(EditText edit) {
return edit.getText() == null || edit.getText().toString().trim().length() <= 0;
} public static boolean hideInputMethod(Activity activity) {
return hideInputMethod(activity, activity.getWindow().getDecorView().getWindowToken());
} public static boolean hideInputMethod(Dialog dialog) {
return hideInputMethod(dialog.getContext(), dialog.getWindow().getDecorView().getWindowToken());
} public static boolean hideInputMethod(Context context, IBinder iBinder) {
InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
return im.hideSoftInputFromWindow(iBinder, 0);
} public static void checkBackgroudThread() {
if (Looper.getMainLooper() == Looper.myLooper()) {
throw new IllegalStateException("It must run in backgroud thread.");
}
} public static void cancelAsyncTask(AsyncTask<?, ?, ?> task) {
if (task != null) {
task.cancel(true);
}
} public static void clearBitmapInImageView(ImageView v) {
if (v != null) {
clearBitmapInDrawable(v.getDrawable());
}
} public static void clearBackgroundBitmapInView(View v) {
if (v != null) {
clearBitmapInDrawable(v.getBackground());
}
} public static void clearBitmapInDrawable(Drawable d) {
if (d != null && d instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
if (bitmap != null) {
Logger.v("luochun", bitmap.toString());
bitmap.recycle();
}
}
} public static Bitmap decodeResourceBitmap(Context context, int resId) {
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is);
}
}

UiUtils的更多相关文章

  1. listview下拉刷新和上拉加载更多的多种实现方案

    listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局        android系统为listview提供了addfootview ...

  2. BaseAdapter的抽取

    为了更方便高效的使用BaseAdapter,特意抽取了一下,下面是简单的结构图: 需要4个类: [MyBaseAdapter3]: public abstract class MyBaseAdapte ...

  3. 使用TabPageIndicator的样式问题

    在使用TabPageIndicator往往会出现一些样式问题,导致看不到字,下面是总结的步骤: 1.布局<LinearLayout xmlns:android="http://sche ...

  4. ExpandableListView实现展开更多和收起更多

    [需求]: 如上面图示 当点开某个一级菜单的时候,其他菜单收起: 子级菜单默认最多5个: 多于5个的显示"展开更多" 点击"展开更多",展开该级所有子级菜单,同 ...

  5. ueditor调用其中的附件上传功能

    ueditor实际上是集成了webuploader, 在做内容发布的时候想既有ueditor又有单独的附件上传按钮,这时再加载一个webuploader就显得过于臃肿了,单独利用ueditor的上传功 ...

  6. selenium元素操作

    1.文本框(text field or textarea) element.sendKeys("test");//在输入框中输入内容: element.clear(); //将输入 ...

  7. Selenium 元素定位

    selenium通过driver.findElement(By selector)来定位元素,selector在selenium-java.jar中,里面的方法一共就8种,如下图: 基本定义: By. ...

  8. 6个强大的AngularJS扩展应用

    本文链接:http://www.codeceo.com/article/6-angularjs-extension.html本文作者:码农网 – 小峰 AngularJS现在非常热门,是Google推 ...

  9. angularjs组件之input mask

    今天将奉献一个在在几个angularjs项目中抽离的angular组件 input mask.在我们开发中经常会对用户的输入进行控制,比如日期,货币格式,或者纯数字格式之类的限制,这就是input m ...

随机推荐

  1. 12个提高Java程序员工作效率的工具

    Java开发者常常都会想办法如何更快地编写Java代码,让开发过程变得更加轻松,更加高效.目前,市面上涌现出越来越多的高效编程工具.团长总结了几个常用的工具,其中包含了大多数开发人员已经使用.正在使用 ...

  2. Servlet和JSP学习总结

    目录 Jsp会被编译成servlet,在页面被第一次访问的时候 Jsp中可以在html页面中嵌入java代码或者引入jsp标签 可以在html中引入自定义标签 Web工程的目录结构 Jsp的注释 Js ...

  3. RabbitMQ消息分发轮询

    一,前言 如果我们一个生产者,对应多个消费者,rabbitmq 会发生什么呢 二,消息分发轮询 前提条件:1个生产者  ---->  多个消费者,且no_ack=True (启动三次生产者) ① ...

  4. Linux ppp 数据收发流程

    转:http://blog.csdn.net/yangzheng_yz/article/details/11526671 PPP (Point-to-Point)提供了一种标准的方法在点对点的连接上传 ...

  5. (6)python基础数据类型

    python的六大标准数据类型 (一)Number   数字类型(int float bool complex) (二)String 字符串类型 (三)List 列表类型 (四)Tuple 元组类型 ...

  6. 自动化测试环境搭建(appium+selenium+python)

    一.需要安装的软件(根据你所需要的版本安装即可,不一定必须按照小编的版本来) JDK:1.8.0_171 Python:3.7.1 node.js:10.13.0 android-sdk_r24.4. ...

  7. log4j2 日志打两遍的问题

    在使用log4j2的时候,一般都需要不同的日志分类打印不同的日志等级,如下面的配置 <!-- 用于指定log4j自动重新配置的监测间隔时间,单位是秒 --> <configurati ...

  8. JLOI2016 侦查守卫

    侦查守卫 小R和B神正在玩一款游戏.这款游戏的地图由 N 个点和 N-1 条无向边组成,每条无向边连接两个点,且地图是连通的.换句话说,游戏的地图是一棵有 N 个节点的树. 游戏中有一种道具叫做侦查守 ...

  9. am335x system upgrade usb wifi rtl8188eus(十九)

    1      Scope of Document This document describes how to port rtl8188eus driver to linux 4.14.y desig ...

  10. HGOI 20191103am 题解

    Problem A number 使用一个$2^k$数集中每个元素的和表示数$n$,不同集合的数目有多少? 对于$100\%$的数据满足$1 \leq n \leq 10^6$ Solution : ...