UI组件之EditText
EditText:一个可以输入的控件
- 常用属性
- 监听事件
- 简单登录界面制作
练习代码
activity_main.xml
<Button android:id="@+id/btn_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="EditText" android:textAllCaps="false"/>
MainActivity.java
public class MainActivity extends AppCompatActivity { private Button mBtnEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnEditText = findViewById(R.id.btn_edittext); mBtnEditText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到EditText页面 Intent intent = new Intent(MainActivity.this, EditTextActivity.class); startActivity(intent); } }); }
activity_edit_text.xml
<?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=".EditTextActivity"> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#008B00" android:hint="用户名" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_margin="10dp" android:drawableLeft="@drawable/icon_user" android:drawablePadding="5dp" android:maxLines="1"/> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#008B00" android:inputType="textPassword" android:hint="密码" android:layout_below="@+id/et_1" android:layout_marginTop="15dp" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_margin="10dp" android:drawableLeft="@drawable/icon_password" android:drawablePadding="5dp" android:maxLines="1"/> <EditText android:id="@+id/et_3" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="16sp" android:textColor="#008B00" android:hint="手机号" android:layout_below="@+id/et_2" android:inputType="number" android:layout_marginTop="15dp" android:background="@drawable/bg_username" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_margin="10dp"/> <Button android:id="@+id/btn_login" android:layout_width="136dp" android:layout_height="wrap_content" android:layout_below="@+id/et_3" android:layout_centerInParent="true" android:layout_marginTop="40dp" android:background="@drawable/bg_btn4" android:text="登录" android:textColor="#000000" android:textSize="20sp" /> </RelativeLayout>
EditTextActivity.java
package com.example.helloworld; 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; import androidx.appcompat.app.AppCompatActivity; public class EditTextActivity extends AppCompatActivity { private Button mBtnLogin; private EditText mEtUserName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_text); mBtnLogin = findViewById(R.id.btn_login); mBtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(EditTextActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); } }); mEtUserName = findViewById(R.id.et_1); mEtUserName.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) { Log.d("edittext",s.toString()); } @Override public void afterTextChanged(Editable s) { } }); } }