Android 開發小經驗2

1.TextView的ellipsize
我們都知道當在TextView中設定ellipsize時,顯示的結果會是縮略顯示,但是比較不好的是
Google默認隻會顯示倆行,如果自己想多顯示的話就必須自定義TextView,為瞭減少開發
過程中的重復工作,我把最近做的項目中的這部分代碼貼出來,如下:
[java]
package com.hustunique.Fuubo.View; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.TextView; 
 
public class WeiboContentText extends TextView{ 
    private String mText; 
 
    public WeiboContentText(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        // TODO Auto-generated constructor stub 
    } 
 
    public WeiboContentText(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub 
    } 
 
    @Override 
    public CharSequence getText() { 
        // TODO Auto-generated method stub 
        return mText; 
    } 
 
    @Override 
    public void setText(CharSequence text, BufferType type) { 
        // TODO Auto-generated method stub 
        mText = (String) text; 
        if (mText.length()>22) { 
            StringBuffer subTextBuffer = new StringBuffer(mText.substring(0, 19)); 
            subTextBuffer.append("…"); 
            text = subTextBuffer; 
        }  
        super.setText(text, type); 
    } 

比較低級,但是感覺還是比較實用
2.設置輸入法彈出後的佈局問題可以試下以下系列方法:
[java]
getWindow().setSoftInputMode(WindowManager.LayoutParams.xxxx); 

3.TextView實現多行本文滾動
  <TextView 
    android:id="@+id/xxx" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"   <!–垂直滾動條 –>
    android:singleLine="false"       <!–實現多行 –>
    android:maxLines="12"            <!–最多不超過12行 –>
    android:textColor="#ffffff"
    />
   當然我們為瞭讓TextView動起來,還需要用到TextView的setMovementMethod方法設置一個滾動實例,代碼如下
TextView tv = (TextView)findViewById(R.id.tvCWJ);  
tv.setMovementMethod(ScrollingMovementMethod.getInstance());

4.EditText中setInputType的妙用
使用該方法我們可以實現諸如隱藏/顯示輸入內容,隱藏鍵盤等等 

摘自  雲卷雲舒 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *