EditText和Button都是TextView的子类,区别在于TextView不可以编辑,而EditText可以编辑。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="用户名"
android:paddingLeft="10dp"
android:textColor="#808080"
android:textSize="16sp"
android:background="@drawable/username_bg"
android:drawableLeft="@drawable/username"
android:drawablePadding="10dp"
android:maxLines="1"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="密码"
android:paddingLeft="10dp"
android:maxLines="1"
android:drawableLeft="@drawable/password"
android:layout_below="@id/username"
android:layout_marginTop="10dp"
android:inputType="numberPassword"
android:background="@drawable/username_bg"
android:drawablePadding="10dp"
android:textColor="#808080"
android:textSize="16sp"
/>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/password"
android:layout_marginTop="10dp"
android:background="@drawable/change_color"
android:text="登录"
android:textColor="#fff"
android:textSize="20sp"
/>
RelativeLayout>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#808080"
android:width="1dp"/>
<corners android:radius="20dp"/>
shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<solid android:color="#00f"/>
<corners android:radius="10dp"/>
shape>
item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#f0f"/>
<corners android:radius="10dp"/>
shape>
item>
selector>
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EditTextActivity extends AppCompatActivity {
private Button btnLogin;
private EditText username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//findViewById方法必须放在setContentView(R.layout.activity_edit_text)后面,不然在btnLogin.setOnClickListener(new View.OnClickListener() 方法中会报空指针
setContentView(R.layout.activity_edit_text);
btnLogin = findViewById(R.id.login);
//定义按钮点击事件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(EditTextActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
}
});
username = findViewById(R.id.username);
//定义EditText文本框的内容改变监听事件
username.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d("username",charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
参考: https://blog.csdn.net/liuxinguang2013/article/details/79248858