为了账号安全,请及时绑定邮箱和手机立即绑定

工具类-AppUtl常用工具汇总

标签:
Android

[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

import android.app.ActivityManager;

import android.content.ComponentName;

import android.content.Context;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.content.pm.PackageManager.NameNotFoundException;

import android.text.Editable;

import android.text.Selection;

import android.text.TextUtils;

import android.view.KeyEvent;

import android.view.View;

import android.view.inputmethod.EditorInfo;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;

import android.widget.TextView;

 

import java.util.List;

import java.util.Timer;

import java.util.TimerTask;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class AppUtil   {

    private AppUtil() {

         

    }

     

    /**

     * 隐藏输入框

     * @param   view view

     */

    public static void hideSoftInput(View   view) {

        if (view != null && view.getWindowToken() != null)   {

            InputMethodManager   imm = (InputMethodManager)   view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

            imm.hideSoftInputFromWindow(view.getWindowToken(),   0);

        }

    }

 

    /**

     * 显示输入框

     * @param   view view

     */

    public static void showSoftInput(final View view) {

        showSoftInput(view,   200);

    }

 

    /**

     * 显示输入框

     * @param   view view

     * @param   delay 延时 毫秒

     */

    public static void showSoftInput(final View view, int delay) {

        view.requestFocus();

        Timer   timer = new Timer();

        timer.schedule(new TimerTask() {

 

            @Override

            public void run() {

                InputMethodManager   m = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

                m.toggleSoftInput(0,   InputMethodManager.HIDE_NOT_ALWAYS);

            }

 

        },   delay);

    }

 

    /**

     * 移动光标到文本框最后

     * @param   editText EditText

     */

    public static void moveSelectionToEnd(EditText   editText) {

        Editable   etext = editText.getText();

        Selection.setSelection(etext,   etext.length());

    }

 

    /**

     * 获取版本号

     * @param   context 上下文对象

     */

    public static int getVersionCode(Context   context) {

        PackageInfo   info = getPackageInfo(context);

        if (info != null) {

            return info.versionCode;

        }

 

        return -1;

    }

     

    /**

     * 获取版本名

     * @return 版本名称

     */

    public static String getVersionName(Context context){

        String   versionName;

         

        //获取版本类型

        PackageManager   manager = context.getPackageManager();

        PackageInfo   info;

        try {

            info   = manager.getPackageInfo(context.getPackageName(), 0);

            versionName   = info.versionName;

        }   catch (NameNotFoundException   e) {

            versionName   = "other";

        }

         

        return versionName;

    }

 

    /**

     * 获取包信息

     * @param   context 上下文对象

     */

    public static PackageInfo getPackageInfo(Context context) {

        try {

            PackageInfo   info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);

            return info;

        }   catch (NameNotFoundException   e) {

        }

 

        return null;

    }

 

    /**

     * 是否包含中文

     * @param   sequence 验证字符串

     * @return 是否包含中文

     */

    public static boolean isContainChinese(String   sequence) {

        final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";

        Pattern   pattern = Pattern.compile(format);

        Matcher   matcher = pattern.matcher(sequence);

        return matcher.find();

    }

 

    /**

     * 判断邮箱格式

     * @param   str 验证字符串

     * @return 是否邮箱

     */

    public static boolean isEmail(String   str) {

        String   check = "\\w+([-.]\\w+)*@\\w+([-]\\w+)*\\.(\\w+([-]\\w+)*\\.)*[a-z]{2,3}$";

        Pattern   regex = Pattern.compile(check);

        Matcher   matcher = regex.matcher(str);

        if (matcher.matches()) {

            return true;

        }   else {

            return false;

        }

    }

 

    /**

     * 验证是否是手机号码

     * @param   str 验证字符串

     * @return 是否手机号

     */

    public static boolean isMobile(String   str) {

        String   NUM = "+86";

        boolean flag = false;

        if (TextUtils.isEmpty(str)) {

            return flag;

        }   else {

            if (str.indexOf(NUM) > -1) {

                str   = str.substring(NUM.length(), str.length());

            }

            if (str.charAt(0) == '0') {

                str   = str.substring(1, str.length());

            }

            String   rex = "^1\\d{10}$";

            str   = removeBlanks(str);

            if (str.matches(rex)) {

                flag   = true;

            }

            return flag;

        }

    }

 

    /**

     * 删除字符串中的空白符

     * @param   content 字符串内容

     * @return   String 删除后的内容

     */

    public static String removeBlanks(String content) {

        if (content == null) {

            return null;

        }

        StringBuffer   buff = new StringBuffer();

        buff.append(content);

        for (int i = buff.length() - 1; i >= 0; i--) {

            if (' ' == buff.charAt(i) || ('\n' == buff.charAt(i)) || ('\t' == buff.charAt(i)) || ('\r' == buff.charAt(i))) {

                buff.deleteCharAt(i);

            }

        }

        return buff.toString();

    }

 

    /**

     * 18位或者15位身份证验证   18位的最后一位可以是字母x

     * @param   text 字符串

     * @return 是否是身份证

     */

    public static boolean personIdValidation(String   text) {

        boolean flag = false;

        String   regx = "[0-9]{17}x";

        String   reg1 = "[0-9]{15}";

        String   regex = "[0-9]{18}";

        flag   = text.matches(regx) || text.matches(reg1) || text.matches(regex);

        return flag;

    }

 

    /**

     * 点击输入法中“下一个”将焦点与光标跳转到下一输入框中

     * @param   currentEt 当前的输入框

     * @param   nextEt 下一个输入框

     */

    public static void setInputType(final EditText currentEt,  final EditText nextEt) {

        currentEt.setSingleLine(true);   // android:singleLine=”true”

        currentEt.setImeOptions(EditorInfo.IME_ACTION_NEXT);

        currentEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_NEXT   || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

                    currentEt.clearFocus();

                    nextEt.requestFocus();

                    return true;

                }

                return false;

            }

        });

    }

 

    /**

     * 获取当前进程名称

     * @param   context context

     * @return 进程名称

     */

    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager   activityManager = (ActivityManager) context

                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo   appProcess : activityManager

                .getRunningAppProcesses())   {

            if (appProcess.pid == pid) {

                return appProcess.processName;

            }

        }

        return null;

    }

 

    /**

     * 判断某个界面是否在前台

     *

     * @param   context context

     * @param   className

     *              某个界面名称

     */

    public static boolean isForeground(Context   context, String className) {

        if (context == null || TextUtils.isEmpty(className)) {

            return false;

        }

 

        ActivityManager   am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        List<activitymanager.runningtaskinfo>   list = am.getRunningTasks(1);

        if (list != null && list.size() > 0) {

            ComponentName   cpn = list.get(0).topActivity;

 

            if (className.equals(cpn.getClassName())) {

                return true;

            }

        }

 

        return false;

    }

 

}</activitymanager.runningtaskinfo>

原文链接:http://www.apkbus.com/blog-139130-62829.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消