Android應用開發之UI組件(TextView;EditText)

來源:互聯網
上載者:User

TextView
屬性設定

android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:id="@+id/autotx" 
注意:setText()或setTextColor()方法的參數是一個int值還是一個資源地址
android:autoLink

<TextView 
 
        android:id="@+id/tvWebUrl" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:autoLink="web" /> 
 
  
 
    <TextView 
 
        android:id="@+id/tvEmail" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:autoLink="email" /> 
 
  
 
    <TextView 
 
        android:id="@+id/tvPhone" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:autoLink="phone" /> 
 
  
 
    <TextView 
 
        android:id="@+id/tvMap" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:autoLink="map" /> 
 
  
 
    <TextView 
 
        android:id="@+id/tvAll" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:autoLink="all" 
 
        android:text="你好,很高興認識你,我的部落格:http://blog.csdn.net/jiahui524。 手機號碼:15580974038.郵箱:272570596@qq.com" /> 
 
  
 
    <TextView 
 
        android:id="@+id/tvHtml" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" /> 
 
  
 
    <TextView 
 
        android:id="@+id/tvHtml1" 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:text="@string/link_text_manual" /> 

private void findViews(){ 
 
   TextView tvWebUrl = (TextView)findViewById(R.id.tvWebUrl); 
 
   tvWebUrl.setText("網易:http://www.163.com"); 
 
    
 
   TextView tvEmail,tvPhone, tvMap ,tvHtml; 
 
    
 
   tvEmail = (TextView) this.findViewById(R.id.tvEmail); 
 
   tvPhone =  (TextView) this.findViewById(R.id.tvPhone); 
 
   tvMap = (TextView) this.findViewById(R.id.tvMap); 
 
   tvHtml = (TextView)this.findViewById(R.id.tvHtml); 
 
    
 
   tvEmail.setText("我的郵箱:drinkeye@163.com"); 
 
   tvPhone.setText("我的電話:500000"); 
 
    
 
   tvHtml.setText(Html.fromHtml("<font size='33' color='#333333'>我<i>愛</i>北</font>京天<b>安</b>門/n <br/>" + 
 
           "<a href='http://www.163.com'>163</a>")); 
 
   } 

<string name="link_text_manual"> 
 
作者部落格: 
 
<a href="http://nokiaguy.blogjava.net"> 
 
http://nokiaguy.blogjava.net 
 
</a> 
 
</string> 
注意:
android:autoLink=”email” :會出現unsupported action,可能是模擬器bug,須探究
另外使用Html.fromHtml時,超連結只具備外觀,不能跳轉
帶邊框的TextView
自訂帶邊框的TextView

package cn.class3g.activity; 
 
  
 
import android.content.Context; 
 
import android.graphics.Canvas; 
 
import android.graphics.Paint; 
 
import android.util.AttributeSet; 
 
import android.widget.TextView; 
 
  
 
public class BorderTextView extends TextView { 
 
  
 
    public BorderTextView(Context context, AttributeSet attr) { 
 
       super(context,attr); 
 
    } 
 
  
 
    public void onDraw(Canvas canvas) { 
 
       super.onDraw(canvas); 
 
  
 
       Paint paint = new Paint(); 
 
  
 
       paint.setColor(android.graphics.Color.GREEN); 
 
       canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); 
 
       canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); 
 
       canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, 
 
              this.getHeight() - 1, paint); 
 
       canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, 
 
              this.getHeight() - 1, paint); 
 
    } 
 
} 

<cn.class3g.activity.BorderTextView 
 
        android:layout_width="wrap_content" 
 
        android:layout_height="wrap_content" 
 
        android:gravity="center" 
 
        android:padding="30dp" 
 
        android:text="xxxxxxxxxxxxx" 
 
        /> 
9-patch工具的使用

<TextView 
 
        android:id="@+id/tvBorder" 
 
        android:layout_width="wrap_content" 
 
        android:layout_height="wrap_content" 
 
        android:text="@string/link_text_manual" 
 
        android:textColor="#00FF00" 
 
        android:background="@drawable/back" /> 
EditText
基本屬性的設定
輸入特定字元

<EditText 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:password="true" 
 
        android:digits="01234" /> 
 
  
 
    <EditText 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:digits="abcd" /> 
 
  
 
    <EditText 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:inputType="number" /> 
 
  
 
    <EditText 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:inputType="textEmailAddress" /> 
 
  
 
    <EditText 
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:numeric="decimal|signed" /> 
EditText中斷行符號鍵的使用
為EditText對象的註冊OnKeyListener事件,實現onKey()方法

<EditText 
 
      android:id="@+id/text1" 
 
      android:layout_width="fill_parent" 
 
      android:layout_height="wrap_content" 
 
      android:text="text1" /> 
 
 
 
  <Button 
 
      android:id="@+id/button1" 
 
      android:layout_width="fill_parent" 
 
      android:layout_height="wrap_content" 
 
      android:visibility="gone" 
 
      android:text="Button" /> 

et.setOnKeyListener(this); 
 
…    
 
public boolean onKey(View view, int keyCode, KeyEvent event) { 
 
       if (keyCode == KeyEvent.KEYCODE_ENTER) { 
 
           btn.setText(et.getText()); 
 
           et.setVisibility(View.GONE); 
 
           btn.setVisibility(View.VISIBLE); 
 
       } 
 
  
 
       return true; 
 
    } 
  
自動完成輸入內容的組件
l  AutoCompleteTextView
l  MultiCompleteTextView

<AutoCompleteTextView  
 
        android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:id="@+id/autotx" 
 
        /> 
 
     
 
   <MultiAutoCompleteTextView  
 
       android:layout_width="fill_parent" 
 
        android:layout_height="wrap_content" 
 
        android:id="@+id/mautotx" 
 
        /> 

public void onCreate(Bundle savedInstanceState) { 
 
      super.onCreate(savedInstanceState); 
 
      setContentView(R.layout.hide); 
 
 
 
      btn = (Button) this.findViewById(R.id.button1); 
 
      et = (EditText) this.findViewById(R.id.text1); 
 
      et.setOnKeyListener(this); 
 
 
 
      autotx = (AutoCompleteTextView) this.findViewById(R.id.autotx); 
 
      String[] s={"a","abc","ab","b","bc","bdad"}; 
 
       
 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,s); 
 
       
 
      autotx.setAdapter(adapter); 
 
           
 
      mautotx = (MultiAutoCompleteTextView) this.findViewById(R.id.mautotx); 
 
      mautotx.setAdapter(adapter); 
 
      mautotx.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 
 
   } 

摘自 瀟洒哥的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.