EditText基本用法

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:text="@string/UserName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textUser"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="29dp"
        android:layout_marginStart="29dp"
        android:layout_marginTop="21dp" />

    <TextView
        android:text="@string/PWD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textPWD"
        android:layout_below="@+id/editName"
        android:layout_marginTop="45dp"
        android:layout_alignLeft="@+id/textUser"
        android:layout_alignStart="@+id/textUser"
        android:textSize="20sp"/>
    <!--ems设置最大显示的宽度3-->
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="3"
        android:layout_marginTop="28dp"
        android:id="@+id/editName"
        android:layout_below="@+id/textUser"
        android:textColorLink="@android:color/black"
        android:lines="@android:integer/config_shortAnimTime"
        android:maxLines="1"
        android:cursorVisible="?attr/isLightTheme"
        android:hint="@string/enterUserName"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editPWD"
        android:layout_below="@+id/textPWD"
        android:layout_alignLeft="@+id/btnRegister"
        android:layout_alignStart="@+id/btnRegister"
        android:textSize="15sp"
        android:hint="@string/enterPWD"
        />

    <Button
        android:text="@string/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnRegister"
        android:layout_marginTop="35dp"
        android:layout_below="@+id/editPWD"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvResult"
        android:layout_below="@+id/btnRegister"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="1dp"
        android:textColor="#FF0000"
        android:textSize="30sp"
        />

</RelativeLayout>


java代码

public class EditText extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final android.widget.EditText editName,editPWD;
        final Button btnRegister;
        final TextView tvResult;
        super.onCreate(savedInstanceState);
        //显示界面与布局文件相关联
        setContentView(R.layout.test2);
        //代码与xml中控件相关联
        editName= (android.widget.EditText) findViewById(R.id.editName);
        editPWD= (android.widget.EditText) findViewById(R.id.editPWD);
        btnRegister=(Button) findViewById(R.id.btnRegister);
        tvResult=(TextView) findViewById(R.id.tvResult);
        btnRegister.setEnabled(false);
        editName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                 if(s.length()!=0){
                     btnRegister.setEnabled(true);
                     }else{
                     btnRegister.setEnabled(false);
                 }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //3.注册事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实现事件
                String result="";
                result="用户名"+editName.getText()+""+"密码"+editPWD.getText();
                tvResult.setText(result+"");

            }
        });


    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值