<?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+"");
}
});
}
}