activity_main:
<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="宽度充满,高度自适应"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="限制输入的字符数为6"
android:maxLength="6"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="只显示单行"
android:singleLine="true" />
<EditText
android:id="@+id/ed5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="只能输入数字"
android:inputType="number"/>
<EditText
android:id="@+id/ed6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="提示信息"/>
<EditText
android:id="@+id/ed7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="左侧有图片"
android:drawableLeft="@drawable/image1"/>
<EditText
android:id="@+id/ed8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="文本框最初内容(含圆角矩形)"
android:background="@drawable/shape"/>
<!-- 高度自适应-->
</LinearLayout>
shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
<corners android:radius="20dp"/>
</shape>
MainActivity.java
package com.example.home2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText ed5=(EditText) findViewById(R.id.ed5);
EditText ed6=(EditText) findViewById(R.id.ed6);
EditText ed7=(EditText) findViewById(R.id.ed7);
EditText ed8=(EditText) findViewById(R.id.ed8);
// 点击事件
ed5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"只允许数字输入",Toast.LENGTH_SHORT).show();
}
});
// 获得焦点
ed6.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Toast.makeText(MainActivity.this,"焦点改变",Toast.LENGTH_SHORT).show();
}
});
// 输入内容以后 按回车 能够反馈输入的内容
ed7.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(MainActivity.this,""+v.getText(),Toast.LENGTH_SHORT).show();
return false;
}
});
// 文本内容改变
ed8.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) {
}
@Override
public void afterTextChanged(Editable s) {
Toast.makeText(MainActivity.this,""+s,Toast.LENGTH_SHORT).show();
}
// s表示显示的内容
});
}
}
SecondActivity.
package com.example.home2;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
// 数据的保存与恢复
}
java