您的位置:首页 > 其它

优化产品使用体验记录一

2015-06-02 21:09 288 查看
Q:软键盘会覆盖屏幕上下面的一片区域,有时候我们使用某个控件(比如EditText)完了,应该自动将此软键盘收起来

A:参考解决方案:http://blog.sina.com.cn/s/blog_87479ba60101akfh.html,我自己使用这一部分就OK了

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//得到InputMethodManager的实例
if (imm.isActive()) {
//如果开启
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
}


Q:有时候控件输入完成后,我们希望失去焦点,但同时别的控件也不要获取到焦点

A:参考解决方案:http://www.tuicool.com/articles/UvyQVj,我自己的部分:XML部分

<RelativeLayout
android:id="@+id/phone_input_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
<!--“增加部分-->
android:focusable="true"
android:focusableInTouchMode="true"
<!--增加部分”-->
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/login_phone_new" />

<EditText
<!--要失去焦点的控件 -->
android:id="@+id/regi_input_phone_num"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="28.5dp"
android:hint="请输入你的手机号码"
android:inputType="number"
android:textColor="#333333"
android:textColorHint="#999999"
android:textSize="15sp"
android:background="@null"/>

</RelativeLayout>


程序:

//                    ETInputPhoneNum.setFocusableInTouchMode(true);
ETInputPhoneNum.clearFocus();


Q:获取验证码短信息的内容,并将其中的5位纯数字验证码读出来写入到控件(EditText)中

A:参考:http://blog.csdn.net/bufanni12/article/details/29804105?utm_source=tuicoolhttp://www.cppblog.com/AutomateProgram/archive/2010/07/21/120930.html

/*
* 监听短信数据库
*/
class SmsContent extends ContentObserver {
private Cursor cursor = null;
private String subString = null;

public SmsContent(Handler handler){
super(handler);
// TODO Auto-generated constructor stub
}

@SuppressWarnings("deprecation")
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// 下面注释掉的是用来获取某一个确定电话号码的短信息,现在这个是获取所有的短信息
cursor = managedQuery(Uri.parse("content://sms/inbox"),
new String[] { "_id", "address", "read", "body" },
/*" address=? and read=?"*/null,
/*new String[] { "10690023192088", "0" }*/null, "_id desc");
if (cursor != null && cursor.getCount() > 0){
ContentValues values = new ContentValues();
values.put("read", "1");
cursor.moveToNext();
int smsbodyColumn = cursor.getColumnIndex("body");
String smsBody = cursor.getString(smsbodyColumn);
if (smsBody.toString().length() > 10) {
subString = smsBody.substring(0, 6);// 获取一段子字符串,用来比较
//                Log.e("test22", subString);
int ret = subString.compareTo("【某某科技】");
if (ret == 0) {
ETInputCertiCode.setText(getDynamicPassword(smsBody));
ETInputCertiCode.clearFocus();
}
}
}

// 在用managedQuery的时候,不能主动调用close()方法, 否则在Android 4.0+的系统上, 会发生崩溃
if (Build.VERSION.SDK_INT < 14) {
cursor.close();
}
}

/**
* 从字符串中截取连续5位数字组合 ([0-9]{" + 5 + "})截取六位数字 进行前后断言不能出现数字 用于从短信中获取动态密码
*
* @param str
*            短信内容
* @return 截取得到的5位动态密码
*/
public String getDynamicPassword(String str) {
// 6是验证码的位数一般为六位
Pattern continuousNumberPattern = Pattern.compile("(?<![0-9])([0-9]{"
+ 5 + "})(?![0-9])");
Matcher m = continuousNumberPattern.matcher(str);
String dynamicPassword = "";
while (m.find()) {
System.out.print(m.group());
dynamicPassword = m.group();
}

return dynamicPassword;
}
}


在对应的Activity或者Fragment中:

<!-- 权限 -->
<!-- 发送短信-->
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- 阅读消息 -->
<uses-permission android:name="android.permission.READ_SMS" />
<!-- 写入消息 -->
<uses-permission android:name="android.permission.WRITE_SMS" />
<!-- 接收消息 -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />


private SmsContent content;

@Override
protected void onCreate(Bundle savedInstanceState) {
... ...
content = new SmsContent(new Handler());
//注册短信变化监听
this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);
... ...
}


@Override
protected void onDestroy() {
super.onDestroy();
this.getContentResolver().unregisterContentObserver(content);
}


另一种广播的方式:http://www.aichengxu.com/view/13311
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: