0%

这里介绍EditText的简单使用方法,EditText是一个文本输入框,我们在与用户交互的时候常常会用到,比如登录、注册等需要用户输入信息的地方。 先看看例子的效果图: 从上到下依次是一个TextView,用于显示输入的字符串;一个EditText,用于输入字符串;一个CheckBox,用于控制隐藏显示输入字符;最后是一个Button,用于控制TextView显示输入的字符串。下面是布局XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView android:id="@+id/tv"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 />
<EditText android:id="@+id/et"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"	
/>

<CheckBox android:id="@+id/cb"	
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="显示或隐藏密码"
/>
<Button android:id="@+id/bt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="显示输入的字符串"
/>
</LinearLayout>

程序代码:

package com.pocketdigi.edit;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class main extends Activity {
    /** Called when the activity is first created. */
    EditText et;
    CheckBox cb;
    TextView tv;
    Button bt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et=(EditText)findViewById(R.id.et);
        et.setTransformationMethod(PasswordTransformationMethod.getInstance());
        //默认隐藏状态,不显示输入的字符,如果默认不隐藏则删除上面这行
        cb=(CheckBox)findViewById(R.id.cb);
        tv=(TextView)findViewById(R.id.tv);
        bt=(Button)findViewById(R.id.bt);
        bt.setOnClickListener(show);
        cb.setOnCheckedChangeListener(listener);
    }
    OnCheckedChangeListener listener=new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                //如果选中,显示密码
            }else{
                 et.setTransformationMethod(PasswordTransformationMethod.getInstance());
                 //否则隐藏密码
            }
            
        }
        
    };
    OnClickListener show=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv.setText(et.getText());
            //点Button后,TextView显示输入的字符串
        }
        
    };
}

文本控件TextView用法: main.xml中:

<TextView android:id="@+id/result"
    <!--设定ID为result,此项可选-->
    android:layout_width="fill_parent" 
    <!--宽度为填满父容器-->
    android:layout_height="wrap_content"
    <!--高度为自动根据内容调整-->
    android:text="@string/height"
    <!--文本控件内容为string.xml中的height-->
    />

string.xml中的height:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="height">身高</string>
</resources>

在程序中修改显示的文本:

private TextView result;
result=(TextView)findViewById(R.id.report_result);
result.setText(CharSequence text);

另外,一直没搞懂CharSequence和String的区别,JAVA手册上定义CharSequence 是 char 值的一个可读序列,此接口对许多不同种类的 char 序列提供统一的只读访问。但在用法上跟String貌似差不多,有了解的兄弟希望能指教下。 setText方法还支持Resources ID,即可以在xml文件中定义字符串,然后在setText(int resid)引用。TextView不支持HTML标签,但如果需要显示链接,可以在布局xml中加入android:autoLink属性,详见在TextView中显示链接 定义颜色