一、layout代码
<?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="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<TextView
android:id="@+id/db"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="112dp"
android:layout_marginTop="25dp"
android:background="@drawable/fj" />
<EditText
android:id="@+id/ed_01"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/db"
android:layout_marginTop="20dp"
android:textColor="#ff0000"
android:textSize="16sp"
android:hint="用户名/账号"
android:inputType="number"
android:drawableLeft="@mipmap/username"
android:background="@drawable/bg_btn03"
/>
<EditText
android:id="@+id/ed_02"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="35dp"
android:textColor="#ff0000"
android:textSize="16sp"
android:layout_below="@+id/ed_01"
android:hint="密码"
android:inputType="textPassword"
android:drawableLeft="@mipmap/passwd"
android:background="@drawable/bg_btn03"
/>
<Button
android:id="@+id/btn_dl"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="点我登录"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_below="@id/ed_02"
android:background="@drawable/bg_btn05"
/>
</RelativeLayout>
二、java代码
package com.relativelayout;
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 EditViewActivity extends AppCompatActivity {
private Button btn_login;
private EditText ed01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_view);
btn_login = findViewById(R.id.btn_dl);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
//弹窗提示
public void onClick(View v) {
Toast.makeText(EditViewActivity.this,"登录成功!",Toast.LENGTH_SHORT).show();
}
});
ed01 = findViewById(R.id.ed_01);
ed01.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
//内容更改正当时捕捉。此处添加后可能会报错,根据报错信息修改即可
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String content = s.toString();
Log.d("editViewUser",content);
Log.d("length:",String.valueOf(content.length()));
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
附图:
本文不再做单独说明,大多属性前面博客已有注释,部分属性望文生义,极少部分属性可自行百度。