Android 開發小經驗2

來源:互聯網
上載者:User

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.設定IME彈出後的布局問題可以試下以下系列方法:
[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的妙用
使用該方法我們可以實現諸如隱藏/顯示輸入內容,隱藏鍵盤等等 


摘自  雲捲雲舒 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.