*****************************自己总结的小技巧**********************************
来源:互联网 发布:网络研修收获与反思 编辑:程序博客网 时间:2024/05/17 01:16
1.更新UI
text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);// 设置大小,单位像素PXtext2.setTextSize(TypedValue.COMPLEX_UNIT_PT, 24);// 设置大小,单位磅PT
2.双击退出键退出(不用额外开线程)
import android.os.Bundle;import android.support.v7.app.AppCompatActivity; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private boolean isFirst =true; private long lastTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { if(isFirst){ Toast.makeText(this,"再按一次退出",Toast.LENGTH_LONG).show(); lastTime=System.currentTimeMillis(); isFirst=false; }else { if ((System.currentTimeMillis()-lastTime)<2000){ this.finish(); }else { Toast.makeText(this,"再按一次退出",Toast.LENGTH_LONG).show(); lastTime=System.currentTimeMillis(); } } } }
3.xml技巧 height=0 weight=1 占据剩余高度
<1>
<android.support.v4.view.ViewPager android:id="@+id/id_viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" ></android.support.v4.view.ViewPager>
<2>
在LinearLayout里面,平分布局VIEW,每一个的权重要相等
如果只有一个组件(Button/其他)使用了Layout_weight属性,并赋值为了1,而Button1和Button3没有设置 Layout_weight这个属性,根据API,可知,他们默认是0
android:layout_weight="1"
当layout_width=“fill_parent”时,weighth 值越小权重越大,优先级越高
4.输入框EditText的技巧
<EditText android:ems="10" // 最大的长度 android:inputType="phone" > // 拨号键盘// 通过EditText获取该控件上显示的文本内容String number = et_number.getText().toString().trim();// .trim():把一段字符的前后空格去掉
5.设置Toast点击事件this的优化
//在外面定义一个Contextprivate Context mContext;//在setContentView(R.layout.activity_main)下面传递一个thismContext = this;// 避免了类似于MainActivity.this的报错
6.通过context对象获取私有目录
//目录地址:/data/data/packagename/filsecontext.getFileDir().getPath();
7.正则表达式
<1>匹配
// reg是匹配的条件,number是要匹配的数字String reg = "1[358][0-9]{9}";String number = "15918880760";System.out.println(Pattern.matches(reg, number));//另一种写法/* * Pattern p = Pattern.compile(reg); * Matcher m = p.matcher(number); * System.out.println(m.matches()); */
<2>分割字符串
String str = "Hello,world hello;world";String reg = "[, ;]";Pattern p = Pattern.compile(reg);// 汇编下 条件String[] a= p.split(str);// 指定要分割的字符串for (String s : a){ System.out.println(s);}
<3>替换字符
String[] names = { "张王好", "王李子", "赵张飞" };String reg = "(.).(.)";Pattern p = Pattern.compile(reg);for (String name : names) { Matcher m = p.matcher(name);// 被替换的字符 String s = m.replaceAll("$1*$2");// 被替换的条件 System.out.println(s);}
<4>查找
String str = "how are you ni hao ma";String reg = "\\b[a-z]{3}\\b";// \\b 是单词边界Pattern p = Pattern.compile(reg);Matcher m = p.matcher(str);while (m.find()) { System.out.println(m.group());// 把适合的分次打印出来 System.out.println(m.start()+":"+m.end());// 开始和结束的位置}
eg.
String src = new String("ab43a2c43d"); System.out.println(src.replace("3","f"));=>ab4f2c4fd. System.out.println(src.replace('3','f'));=>ab4f2c4fd. System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.System.out.println(src.replaceAll("a","f"));=>fb43fc23d. System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d System.out.println(src.replaceFirst("4","h"));=>abh32c43d.
8.Android常见显示单位
1、px (pixels)像素 一般HVGA代表320x480像素,这个用的比较多。 (一个组件不同像素,在分辨率不同的手机上,显示效果不一样 ,低分辨率的手机,看起来比较大,高分辨率手机看起来比较小,用户体验不一致。)2、dip或dp (device independent pixels)设备独立像素 这个和设备硬件有关,一般为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。3、sp (scaled pixels — best for text size)比例像素 主要处理字体的大小,可以根据系统的字体自适应。
为了适应不同分辨率,不同的像素密度,推荐使用dip ,文字使用sp。
一般组件的宽度和高度用dp,文字用sp
9.带阴影的TextView xml
//涉及到的几个属性:android:shadowColor//设置阴影颜色,需要与shadowRadius一起使用哦!android:shadowRadius//设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0android:shadowDx//设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置android:shadowDy//设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
10.**
0 0
- *****************************自己总结的小技巧**********************************
- javascript 小技巧(自己总结)
- 自己总结的一些安卓的实用小技巧
- c#自己总结的几个常用的小技巧
- 自己总结的在编程中的一些小技巧
- 自己总结的小知识点
- 自己总结的小tips
- 总结自己使用shell命令行常用到的8个小技巧
- 提高自己工作效率的几个小技巧
- js小技巧(自己遇到的一些技巧)
- 自己总结的一些VC常用技巧
- 总结自己常用的UltraEdit使用技巧
- 总结自己常用的UltraEdit使用技巧
- 自己学习总结的小知识
- 自己总结的 Makefile 小模板
- 工作中总结的编程小技巧
- 工作中总结的编程小技巧
- 以前总结的需求调查小技巧
- 个人对移动App的看法
- Some urls - Web relating
- listview含有radiobtn,点击实现单选
- WIFI的Log切入点
- 2017届毕业生安卓面试题-JAVA篇
- *****************************自己总结的小技巧**********************************
- 24 AIDL案例
- hd 1065 I Think I Need a Houseboat(数学、水)
- Python代理+线程+进程+socket
- HDU 5381The sum of gcd 莫队算法
- 可变参数【Java】
- LA2995- Image Is Everything
- MDK在链接时提示空间不够(No space in execution regions with .ANY selector... )的解决方案总结
- lua封装的位运算