安卓表单
2023年11月6日表单逻辑层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MetaDataActivity07 extends AppCompatActivity implements View.OnFocusChangeListener{ private EditText et_phone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meta_data07); et_phone = findViewById(R.id.et_phone); EditText et_password = findViewById(R.id.et_password); et_password.setOnFocusChangeListener(this); } @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { String phone = et_phone.getText().toString(); // 手机号码不足11位 if (TextUtils.isEmpty(phone) || phone.length() < 11) { // 手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框 et_phone.requestFocus(); Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show(); } } } } |
视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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" android:padding="5dp"> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入11位手机号码" android:inputType="number" android:maxLength="11" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入6位密码" android:inputType="numberPassword" android:maxLength="6" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout> |
输入自动隐藏输入法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import com.example.myapplication.util.ViewUtil; public class MetaDataActivity07 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meta_data07); EditText et_phone = findViewById(R.id.et_phone); EditText et_password = findViewById(R.id.et_password); et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11)); et_password.addTextChangedListener(new HideTextWatcher(et_password, 6)); } // 定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法 private class HideTextWatcher implements TextWatcher { // 声明一个编辑框对象 private EditText mView; // 声明一个最大长度变量 private int mMaxLength; public HideTextWatcher(EditText v, int maxLength) { this.mView = v; this.mMaxLength = maxLength; } @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) { // 获得已输入的文本字符串 String str = s.toString(); // 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法 if (str.length() == mMaxLength) { // 隐藏输入法软键盘 ViewUtil.hideOneInputMethod(MetaDataActivity07.this, mView); } } } } |
util 单元方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.example.myapplication.util; import android.app.Activity; import android.content.Context; import android.view.View; import android.view.inputmethod.InputMethodManager; public class ViewUtil { public static void hideOneInputMethod(Activity act, View v) { // 从系统服务中获取输入法管理器 InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE); // 关闭屏幕上的输入法软键盘 imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } |
视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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" android:padding="5dp"> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@drawable/editext_selector" android:hint="输入11位时自动隐藏输入法" android:inputType="text" android:maxLength="11" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@drawable/editext_selector" android:hint="输入6位时自动隐藏输入法" android:inputType="textPassword" android:maxLength="6" /> </LinearLayout> |
确认弹框开始
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.example.myapplication; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MetaDataActivity07 extends AppCompatActivity implements View.OnClickListener { private TextView tv_alert; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meta_data07); findViewById(R.id.btn_alert).setOnClickListener(this); tv_alert = findViewById(R.id.tv_alert); } @Override public void onClick(View v) { // 创建提醒对话框的建造器 AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置对话框的标题文本 builder.setTitle("尊敬的用户"); // 设置对话框的内容文本 builder.setMessage("你真的要卸载我吗?"); // 设置对话框的肯定按钮文本及其点击监听器 builder.setPositiveButton("残忍卸载", (dialog, which) -> { tv_alert.setText("虽然依依不舍,但是只能离开了"); }); // 设置对话框的否定按钮文本及其点击监听器 builder.setNegativeButton("我再想想", (dialog, which) -> { tv_alert.setText("让我再陪你三百六十五个日夜"); }); // 根据建造器构建提醒对话框对象 AlertDialog dialog = builder.create(); // 显示提醒对话框 dialog.show(); } } |
日期选择器
逻辑层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import java.util.Calendar; public class MetaDataActivity07 extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener { private DatePicker dp_date; private TextView tv_date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meta_data07); findViewById(R.id.btn_ok).setOnClickListener(this); findViewById(R.id.btn_date).setOnClickListener(this); tv_date = findViewById(R.id.tv_date); dp_date = findViewById(R.id.dp_date); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_ok: String desc = String.format("您选择的日期是%d年%d月%d日", dp_date.getYear(), dp_date.getMonth() + 1, dp_date.getDayOfMonth()); tv_date.setText(desc); break; case R.id.btn_date: // 获取日历的一个实例,里面包含了当前的年月日 /*Calendar calendar = Calendar.getInstance(); calendar.get(Calendar.YEAR); calendar.get(Calendar.MONTH); calendar.get(Calendar.DAY_OF_MONTH);*/ DatePickerDialog dialog = new DatePickerDialog(this, this, 2090, 5, 11); // 显示日期对话框 dialog.show(); break; } } @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { String desc = String.format("您选择的日期是%d年%d月%d日", year, month + 1, dayOfMonth); tv_date.setText(desc); } } |
视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?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" android:padding="5dp"> <Button android:id="@+id/btn_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择日期" /> <DatePicker android:id="@+id/dp_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:calendarViewShown="false" android:datePickerMode="spinner" /> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确 定" /> <TextView android:id="@+id/tv_date" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |
时间选择器
逻辑层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; public class MetaDataActivity07 extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener { private TimePicker tp_time; private TextView tv_time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_meta_data07); findViewById(R.id.btn_ok).setOnClickListener(this); findViewById(R.id.btn_time).setOnClickListener(this); tp_time = findViewById(R.id.tp_time); tp_time.setIs24HourView(true); tv_time = findViewById(R.id.tv_time); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_ok: String desc = String.format("您选择的时间是%d时%d分", tp_time.getHour(), tp_time.getMinute()); tv_time.setText(desc); break; case R.id.btn_time: // 获取日历的一个实例,里面包含了当前的时分秒 Calendar calendar = Calendar.getInstance(); // 构建一个时间对话框,该对话框已经集成了时间选择器。 TimePickerDialog dialog = new TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog, this, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true); // true表示24小时制,false表示12小时制 dialog.show(); break; } } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String desc = String.format("您选择的时间是%d时%d分", hourOfDay, minute); tv_time.setText(desc); } } |
视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?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" android:padding="5dp"> <Button android:id="@+id/btn_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择时间" /> <TimePicker android:id="@+id/tp_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:timePickerMode="spinner" /> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确 定" /> <TextView android:id="@+id/tv_time" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |