`

android 编程技巧汇总

 
阅读更多

android的EditText,转到某个Activity时,总是因为它而弹出该输入法,那么一般是我们点击了它的输入框后才弹出,所以可以在oncCreate中添加如下代码:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
 

 

  如何使得android listview在刷新之后仍然定位到原来的地方,那就需要知道两个量,一个是你定位到的item的Index,和adapter上的编号一致,一个是从listview到该item的距离,以下是方法:

 

// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// ...

// restore
mList.setSelectionFromTop(index, top);

 其中index即为listview中顶层一个可视的item。而v就是该可视item。因为getChildAt返回的是当前可视的item,参数的意思是可视item重新排列后的index,所以是0.

 

使得Toast 显示在某个View之上:

步骤:获取该view在全屏幕上的绝对location。然后获取状态栏的高度,将location减去该高度后便可以设置Toast在该view上。

 

int[] location = new int[2];
fooView.getLocationOnScreen(location);
Rect rect=new Rect();
fooActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
Toast toast=Toast.makeText(...);
toast.setGravity(Gravity.TOP|Gravity.LEFT,location[0],location[1]-rect.top);
toast.show();

 从以上可以得知toast的setGravity设置的x,y是从标题栏开始的坐标(若有的话),而getLocationOnScreen获取的是该view在整个屏幕的坐标。而且,默认下,Toast的重心是Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM

 

TextView 使用html的标签:

 

Spanned sp=android.text.html.fromHtml("<b>jason</b>");
yourTextView.setText(sp);

TextView中的字体加粗(中文字体)

 TextView tv = (TextView)findViewById(R.id.TextView01);
                                      TextPaint tp = tv.getPaint();
                                      tp.setFakeBoldText(true);

 

Spannable string 常用方法:来自http://mycoding.iteye.com/blog/1297490

 

 

TextView txtInfo =(TextView)findViewById(R.id.tv);
        //SpannableString文本类,包含不可变的文本但可以用已有对象替换和分离。
        //可变文本类参考SpannableStringBuilder
        SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");  
        //用颜色标记文本
        ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,  
                //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用超链接标记文本
        ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,  
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用样式标记文本(斜体)
        ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,  
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用删除线标记文本
        ss.setSpan(new StrikethroughSpan(), 7, 10,  
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用下划线标记文本
        ss.setSpan(new UnderlineSpan(), 10, 16,  
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //用颜色标记
        ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13,  
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //获取Drawable资源
        Drawable d = getResources().getDrawable(R.drawable.icon);  
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        //创建ImageSpan
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        //用ImageSpan替换文本
        ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);  
        txtInfo.setText(ss);
        txtInfo.setMovementMethod(LinkMovementMethod.getInstance());     
 

 

 

反向格式化字符串化的日期:

 

String myTime="2012/03/14 02:30 pm";
Date date=new SimpleDateFormate("yyyy/MM/dd hh:mm a",Locale.ENGLISH).parse(myTime);

 其中加入语言名称是为了确定a处代表的是pm/am还是别国的语言(如中文的下午/上午),若你的系统是英文,此处可以不表示,若是中文,则会报错,因为这涉及到编码的问题。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics