<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/ubbg"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:paddingLeft="50dp"
android:textColor="#ffffff"
android:textSize="30sp"
android:hint="请输入密码"
android:textColorHint="#999999"
android:maxLines="1"
android:inputType="textPassword"
android:maxLength="6"
/>
</RelativeLayout>
效果图:
解析:
加深:
package echang.pxd.a3edittext;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener {
//1.xml配置文件
//2.使用代码创建
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id获取xml里对应的控件
EditText et = findViewById(R.id.et_password);
//监听键盘的key按下的事件
//1.创建匿名类对象
//1.1 创建一个类管理事件
/*
PXDListener pl = new PXDListener();
et.setOnEditorActionListener(pl);
*/
//et.setOnEditorActionListener(new PXDListener());
//1.2 匿名类对象
/*
et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
return false;
}
});
*/
//1.3 Lambda表达式
et.setOnEditorActionListener((textView,actionId,event) -> {
System.out.println("点击了");
return true;
});
//2.当前这个Activity来监听事件
//et.setOnEditorActionListener(this);
}
//键盘被按下的回调事件
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
System.out.println("点击了");
return false;
}
}
//创建一个类 管理事件的回调
class PXDListener implements TextView.OnEditorActionListener{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
System.out.println("点击了");
return false;
}
}