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. (备忘)Linux mount(挂载命令)详解

    挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...

  2. 08_Redis通用命令

    keys pattern:获取所有与pattern匹配的key,返回所有与该key匹配的keys:通配符:*表示任意0个或多个任意字符,?表示任意一个字符

  3. JAVA8初探-让方法参数具备行为能力并引入Lambda表达式

    关于JAVA8学习的意义先来贴一下某网站上的对它的简单介绍:“Java 8可谓Java语言历史上变化最大的一个版本,其承诺要调整Java编程向着函数式风格迈进,这有助于编写出更为简洁.表达力更强,并且 ...

  4. 关于WebMvcConfigurationSupport的大坑-静态资源访问不了

    WebMvcConfigurationSupport是spring boot2.0以后用来替代WebMvcConfigurerAdapter,但是如果你直接用WebMvcConfigurationSu ...

  5. Java8-Stream-No.12

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.UUID; i ...

  6. BZOJ 1982 / Luogu SP2021: [Spoj 2021]Moving Pebbles (找平衡状态)

    这道题在论文里看到过,直接放论文原文吧 在BZOJ上是单组数据,而且数据范围符合,直接int读入排序就行了.代码: #include <cstdio> #include <algor ...

  7. @Import注解的作用

    在@Import注解的参数中可以填写类名,例如@Import(Abc.class),根据类Abc的不同类型,spring容器有以下四种处理方式: 1. 如果Abc类实现了ImportSelector接 ...

  8. 【git】git中使用https和ssh协议的区别以及它们的用法

    git可以使用四种主要的协议来传输资料: 本地协议(Local),HTTP 协议,SSH(Secure Shell)协议及 git 协议.其中,本地协议由于目前大都是进行远程开发和共享代码所以一般不常 ...

  9. OFDM符号速率与子载波间隔的关系

    一般采样速率fs等于符号速率Rb: 采样速率表示采样的快慢,现有N个采样点: 类似于路程(N)除以速度(fs)等于时间(Tb): 采样一个点所需时间Tb = N/fs: 即一个子载波持续时间为Tb = ...

  10. Luogu P1641 [SCOI2010]生成字符串 组合数学

    神仙.... 当时以为是,$x$代表$1$,$y$代表$0$,所以不能过$y=x$的路径数...结果不会... 然后康题解...ヾ(。`Д´。)竟然向右上是$1$,向右下是$0$.... 所以现在就是 ...