Android成长的故事——Android组件_ TextView && EditView

Android组件中TextView是最常见的一种组件,虽然常用,但不可或缺。

UI组件TextView

属性:android:autoLink

我们用一个实例来解释这个属性

首先在strings.xml里写出我们需要的字符串。

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="app_name">TextViewTest</string>
    <string name="webUrl">百度:http://baidu.com</string>
    <string name="email">我的:296463139@qq.com</string>
    <string name="phoneNumber">电话号码:10086</string>
    <string name="autoSAll">百度:http://baidu.com 我的:296463139@qq.com</string>
 
</resources>

然后在我们的main.xml里声明属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="@string/webUrl" />
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="email"
        android:text="@string/email" />
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="@string/phoneNumber" />
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="@string/autoSAll" />
 
    <TextView
        android:id="@+id/tvId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
 
</LinearLayout>

-------------------Activity------------

package cn.class3g.activity;
 
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
 
public class TextViewTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
 
       TextView tv = (TextView) this.findViewById(R.id.tvId);
       String htmlStr = "<font size='30' color='#00FF22'>我</font>爱<b>你</b>"
              + "<a href='baidu.com'>百度</a>";
       tv.setText(Html.fromHtml(htmlStr));
    }
}

注意:

android:autoLink=”email” :会出现unsupported action,可能是模拟器bug,须探究。

另外使用Html.fromHtml时,超链接只具备外观,不能跳转

效果图:


自定义带边框的TextView

------------------------Activity------------------

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 MyBorderTextView extends TextView{
 
      public MyBorderTextView(Context context, AttributeSet attrs) {
           super(context, attrs);
          
      }
      @Override
      protected void onDraw(Canvas canvas) {
           super.onDraw(canvas);
          
           Paint paint = new Paint();
          
           paint.setColor(android.graphics.Color.YELLOW);
          
 
           canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);
//1、横坐标0到this.getWidth()-1,纵坐标0到0
           canvas.drawLine(0, 0, 0, this.getHeight()-1, paint);
//2、横坐标0到0,纵坐标0到this.getHeight()-1
           canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);
//3、横坐标this.getWidth()-1到this.getWidth()-1,纵坐标0到this.getHeight()-1
           canvas.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);
//4、横坐标0到this.getWidth()-1,纵坐标this.getHeight()-1到this.getHeight()-1
      }
 
}

然后只需要在布局里调用这个就行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <cn.class3g.activity.MyBorderTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="30dp"
        android:text="hello"
        android:textColor="#cccccc" >
    </cn.class3g.activity.MyBorderTextView>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:padding="30dp"
        android:text="hello hello hello hello"
        android:background="@drawable/ic_launcher"
        android:textColor="#cccccc" >
    </TextView>
 
</LinearLayout>
效果图:

输入特定字符

效果图:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="1" >
 
        <TableRow >
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/user" />
 
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
 
        <TableRow >
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/pass" />
 
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPassword" />
        </TableRow>
 
        <TableRow >
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/twopass" />
 
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textPassword" />
        </TableRow>
 
        <TableRow >
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/email" />
 
            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress" />
        </TableRow>
 
        <TableRow >
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/authcode" />
 
            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
 
                <TableRow >
                    <EditText
                        android:layout_width="80dp"
                        android:layout_height="wrap_content" />
 
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/p2" />
 
                    <TextView
                        android:id="@+id/changeId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/change" />
                </TableRow>
            </TableLayout>
        </TableRow>
 
        <TableRow >
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
 
            <CheckBox
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/text"
                android:id="@+id/textId"
                 />
        </TableRow>
    </TableLayout>
 
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="提交注册" />
    </RelativeLayout>
 
</LinearLayout>

--------------------------strings.xml---------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <string name="hello">Hello World, ZhuCeActivity!</string>
    <string name="app_name">ZhuCe</string>
    <string name="user">用户名:</string>
    <string name="pass">密码:</string>
    <string name="twopass">确认密码:</string>
    <string name="email">电子邮箱:</string>
    <string name="authcode">验证码:</string>
    <string name="change">看不清?<a href="#">换一个</a></string>
    <string name="text">我已经阅读并同意协议</string>
   
</resources>

自动完成输入内容的组件

package cn.class3g.activity;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
 
public class AutoActivity extends Activity{
   AutoCompleteTextView auto = null;
   MultiAutoCompleteTextView mauto = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        String[] str = {"abc","add","aas","afr","bdr","ber","bsd"};
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line,str);
       
        auto.setAdapter(adapter);
       
        //----------------------
        mauto.setAdapter(adapter);
        mauto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
       
    }
    private void findViews(){
      auto = (AutoCompleteTextView) this.findViewById(R.id.autoId);
      mauto = (MultiAutoCompleteTextView) this.findViewById(R.id.mautoId);
    }
}

EditText中回车键的使用

EditText对象的注册OnKeyListener事件,实现onKey()方法

package cn.class3g.activity;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
 
public class EditTextTest1Activity extends Activity implements OnKeyListener{
    /** Called when the activity is first created. */
   Button but = null;
   EditText et = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onkey);
        findView();
    }
    public void findView(){
      but = (Button) this.findViewById(R.id.buttonId);
      et = (EditText) this.findViewById(R.id.edit);
      et.setOnKeyListener(this);
    }
 
   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
      // TODO Auto-generated method stub
      if(keyCode==KeyEvent.KEYCODE_ENTER){
        but.setText(et.getText());
        et.setVisibility(View.GONE);
        but.setVisibility(View.VISIBLE);
      }
      return false;
   }
}

2011年12月13日 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值