Android 开发过程遇到的经验总结

来源:互联网 发布:阿里云 上海机房 编辑:程序博客网 时间:2024/05/24 06:33

1、android get 请求包含特殊字符或中文的时候需要转码。

//转码
String url= java.net.URLEncoder.encode(ss,"utf-8");
System.out.println(url);
//解码
System.out.println(java.net.URLDecoder.decode(url,"utf-8"));
2、在我们的项目中,经常需要对WebView中加载的内容是否完成或者是否加载失败进行判别,这两天写的一个小应用就涉及到了这一点。WebView的实质就是loadUrl,所以前提是设备接入网络,那么这个如何判断呢?看代码吧:

// 网络状态public boolean isNetworkConnected(Context context) {    if (context != null) {        ConnectivityManager mConnectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo mNetworkInfo = mConnectivityManager                .getActiveNetworkInfo();        if (mNetworkInfo != null) {            return mNetworkInfo.isAvailable();        }    }    return false;
如果网络正常就加载WebView,否则就提示用户接入网络。

问题2:在使用WebView的过程中,我发现在当前Activity加载网页之外,还会打开浏览器加载我们的URL。所以需要重写一个

wvLast.setWebViewClient(new WebViewClient() {    public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;    }}
3、软键盘的显示与隐藏

public static void showSoftKeybord(Context context,EditText edit){InputMethodManager inputManager = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);inputManager.showSoftInput(edit, 0);}public static void hideSoftKeybord(Context context){try{((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}catch (Exception e) {e.printStackTrace();}}
4、判断控件是否处于显示状态

可以通过view.getVisiable()然后与View.VISIBLE、View.INVISIBLE、View.GONE比较

5、获取手机的产品信息

String phoneInfo = "产品名称: " + android.os.Build.PRODUCT;phoneInfo += "\nCPU型号: " + android.os.Build.CPU_ABI;phoneInfo += "\n标签: " + android.os.Build.TAGS;phoneInfo += "\n手机型号: " + android.os.Build.MODEL;phoneInfo += "\nSDK版本: " + android.os.Build.VERSION.SDK;phoneInfo += "\n系统版本: " + android.os.Build.VERSION.RELEASE;phoneInfo += "\n设备驱动: " + android.os.Build.DEVICE;phoneInfo += "\n显示: " + android.os.Build.DISPLAY;phoneInfo += "\n品牌: " + android.os.Build.BRAND;phoneInfo += "\n主板: " + android.os.Build.BOARD;phoneInfo += "\n指纹: " + android.os.Build.FINGERPRINT;phoneInfo += "\nID: " + android.os.Build.ID;phoneInfo += "\n制造商: " + android.os.Build.MANUFACTURER;phoneInfo += "\n用户组: " + android.os.Build.USER;return phoneInfo;
6、在java文件中声明一个TextView的字体颜色

Color.parse("#eeeeee");

7、Android判断输入类型

String text= edInput.getText().toString();
Pattern p =Pattern.compile("[0-9]*");
Matcher m = p.matcher(text);
if(m.matches()){
Toast.makeText(Main.this,"输入的是数字",Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[a-zA-Z]");
m=p.matcher(text);
if(m.matches()){
Toast.makeText(Main.this,"输入的是字母",Toast.LENGTH_SHORT).show();
}
p=Pattern.compile("[\u4e00-\u9fa5]");
m=p.matcher(text);
if(m.matches()){
Toast.makeText(Main.this,"输入的是汉字",Toast.LENGTH_SHORT).show();
}


8、popupWindow 在控件的各个方向上的显示(上、下、左、右)

上方:  int[] location =newint[2];
v.getLocationOnScreen(location);
        popupWindow.showAtLocation(v,Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());

下方:popupWindow.showAsDropDown(v);

左边:popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);  

右边:popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);  


9、设置TextView中给字体设置不同的颜色

 String str = "<font color='#ff0000'><b>hello world</b></font>牛兑换<font color='#ff0000'><b>我爱你</b></font>";
         aq.id(R.id.html).text(Html.fromHtml(str));


10、TextView的灵活使用:

在项目中有无遇到过这样一种程况,例如文字"王明今年10岁了", 但是数字10是从网络返回的数据, 而你又想把这个文字写在xml中, 过往我的做法是分成3个TextView, 实现愚蠢。今天遇到一种解决办法。

在string.xml中设置

<string name="age">王明今年 %1$d</string>


然后设置的时候就可以:

textView.setText(String.format(mContext.getString(R.string.age), age));

 

%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 %n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0 %n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00 
11、在多行文本输入的时候,Textview里面的文本默认居中对齐的,这样子有点个跟日常的习惯不太一样。所以为了解决这个问题,通过添加
android:gravity:start。便可解决问题。
12、通知listView的适配器数据变更

这种适用于listView追加数据的情况,严格说不是恢复listView滚动的位置,只是保持滚动位置不错

  1. listDataAdapter.getDataList.addAll(newDataList());   
  2. listDataAdapter.notifyDataSetChanged();  

13、去除listview顶部与底部分割线
最简单的办法是让listView直接高度使用wrap_content。注意的是这时候他的父容器不能使用PaddingButtom属性。
         前提是设置了此方法
addHeaderView(headView, null, true);

        addFooterView(footView, null, true);

        注意:第三个参数必须为true,否则无效

        android:footerDividersEnabled="false"

        android:headerDividersEnabled="false"



0 0
原创粉丝点击