1.焦点变更监听器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".EditFocusActivity"android:padding="5dp"><EditTextandroid:id="@+id/et_phone"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入11位手机号码"android:inputType="text"android:maxLength="11"android:padding="6dp"android:layout_margin="5dp"android:background="@drawable/edit_select"/><EditTextandroid:id="@+id/et_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入密码"android:padding="6dp"android:inputType="numberPassword"android:layout_margin="5dp"android:background="@drawable/edit_select"android:maxLength="11"/><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_margin="5dp"android:layout_height="wrap_content"android:text="登录"/></LinearLayout>
package com.tiger.chapter05;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {private EditText et_phone;private EditText et_password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_edit_focus);et_phone = findViewById(R.id.et_phone);et_password = findViewById(R.id.et_password);et_password.setOnFocusChangeListener(this);}@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus){String phone = et_phone.getText().toString();if (phone==null||phone.length()<11){//手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框et_phone.requestFocus();Toast.makeText(this,"请输入11位手机号码",Toast.LENGTH_SHORT).show();//short短时间小时}}}
}
2.文本变化监听器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"android:padding="5dp"tools:context=".EditHideActivity"><EditTextandroid:id="@+id/et_phone"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入11位时自动隐藏输入法"android:layout_margin="5dp"android:padding="5dp"android:background="@drawable/edit_select"android:inputType="text"android:maxLength="11"/><EditTextandroid:id="@+id/et_password"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入6位自动隐藏输入法"android:layout_margin="5dp"android:padding="5dp"android:inputType="textPassword"android:background="@drawable/edit_select"android:maxLength="6"/></LinearLayout>
package com.tiger.chapter05.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 view){//从系统服务中获取输入法管理器InputMethodManager manager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);//关闭屏幕上的输入法软键盘manager.hideSoftInputFromWindow(view.getWindowToken(), 0);}
}
package com.tiger.chapter05;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;import com.tiger.chapter05.util.ViewUtil;public class EditHideActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_edit_hide);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 Integer maxLength;public HideTextWatcher(EditText mView, Integer maxLength) {this.mView = mView;this.maxLength = maxLength;}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {//获得已输入的文本字符串String str = s.toString();//输入文本达到11位 (如手机号码),或者达到6位 (如登录密码)时 关闭输入法if (str.length()== maxLength){ViewUtil.hideOneInputMethod(EditHideActivity.this,mView);}}}
}
3. 提醒对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"android:padding="5dp"tools:context=".CheckBoxActivity"><Buttonandroid:id="@+id/btn_alert"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="弹出提醒对话框" /><TextViewandroid:id="@+id/tv_alert"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp" /></LinearLayout>
package com.tiger.chapter05;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 AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {private TextView tv_alert;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_alert_dialog);findViewById(R.id.btn_alert).setOnClickListener(this);tv_alert = findViewById(R.id.tv_alert);}@Overridepublic void onClick(View v) {//创建提醒对话框的建造器//这个Builder是个静态内部类AlertDialog.Builder builder = new AlertDialog.Builder(this)//将上下文放进去.setTitle("尊敬的用户")//设置对话框的标题文本.setMessage("你真的要卸载我吗?")//设置对话框的内容文本.setPositiveButton("残忍卸载", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {tv_alert.setText("虽然依依不舍,但是只能离开了");}})//设置对话框的肯定按钮文本及其点击监听器.setNegativeButton("我再想想", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {tv_alert.setText("让我再陪你三百六十五个日夜");}});//设置对话框的否定按钮文本及其点击监听器//根据建造起创建提醒对话框对象AlertDialog alertDialog = builder.create();//显示提醒对话框alertDialog.show();}
}
4.日期对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"android:padding="5dp"tools:context=".DatePickerActivity"><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><DatePickerandroid:id="@+id/dp_date1"android:layout_width="match_parent"android:layout_height="wrap_content"android:datePickerMode="spinner"android:calendarViewShown="false"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"><DatePickerandroid:id="@+id/dp_date2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:datePickerMode="calendar"/></LinearLayout><Buttonandroid:id="@+id/btn_ok"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="确定" /><TextViewandroid:id="@+id/tv_date"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/btn_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="弹出日期对话框" /></LinearLayout></ScrollView></LinearLayout>
package com.tiger.chapter05;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener {private DatePicker dp_date1;private DatePicker dp_date2;private TextView viewById;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_date_picker);findViewById(R.id.btn_ok).setOnClickListener(this);dp_date1 = findViewById(R.id.dp_date1);dp_date2 = findViewById(R.id.dp_date2);viewById = findViewById(R.id.tv_date);button = findViewById(R.id.btn_date);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_ok) {//月份 是0开始 所以加一String desc = String.format("您选择的日期是 %d年%d月%d日", dp_date1.getYear(), dp_date1.getMonth()+1, dp_date1.getDayOfMonth())+"\n";String desc2 = String.format("您选择的日期是 %d年%d月%d日", dp_date2.getYear(), dp_date2.getMonth()+1, dp_date2.getDayOfMonth());String result = desc+desc2;viewById.setText(result);}else {}}
}
package com.tiger.chapter05;import androidx.appcompat.app.AppCompatActivity;import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;import java.util.Calendar;public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {private DatePicker dp_date1;private DatePicker dp_date2;private TextView viewById;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_date_picker);findViewById(R.id.btn_ok).setOnClickListener(this);dp_date1 = findViewById(R.id.dp_date1);dp_date2 = findViewById(R.id.dp_date2);viewById = findViewById(R.id.tv_date);button = findViewById(R.id.btn_date);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_ok) {//月份 是0开始 所以加一String desc = String.format("您选择的日期是 %d年%d月%d日", dp_date1.getYear(), dp_date1.getMonth() + 1, dp_date1.getDayOfMonth()) + "\n";String desc2 = String.format("您选择的日期是 %d年%d月%d日", dp_date2.getYear(), dp_date2.getMonth() + 1, dp_date2.getDayOfMonth());String result = desc + desc2;viewById.setText(result);} else {//获取日历的一个实例 ,里面包含了当前的年月日Calendar instance = Calendar.getInstance();int year = instance.get(Calendar.YEAR);int month = instance.get(Calendar.MONTH);int day = instance.get(Calendar.DATE);//不用减1DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);//显示日期对话框datePickerDialog.show();}}@Overridepublic void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {String result = String.format("您选择的日期是 %d年%d月%d日", year, month + 1, dayOfMonth);viewById.setText(result);}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"android:padding="5dp"tools:context=".DatePickerActivity"><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><DatePickerandroid:id="@+id/dp_date1"android:layout_width="match_parent"android:layout_height="wrap_content"android:calendarViewShown="false"android:datePickerMode="spinner" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"><DatePickerandroid:id="@+id/dp_date2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:datePickerMode="calendar" /></LinearLayout><Buttonandroid:id="@+id/btn_ok"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="确定" /><TextViewandroid:id="@+id/tv_date"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="弹出日期对话框" /></LinearLayout></ScrollView></LinearLayout>
TimePicker&TimePickerDialog
TimePicker 有两种 clock spinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".TimePickerActivity"><Buttonandroid:id="@+id/btn_time"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="请选择时间" /><TimePickerandroid:id="@+id/tp_date"android:layout_width="match_parent"android:layout_height="wrap_content"android:timePickerMode="spinner" /><TextViewandroid:id="@+id/tv_date"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn_d"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="时间对话框" /></LinearLayout>
package com.tiger.chapter05;import androidx.appcompat.app.AppCompatActivity;import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {private TimePicker timePicker;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_time_picker);findViewById(R.id.btn_time).setOnClickListener(this);findViewById(R.id.btn_d).setOnClickListener(this);timePicker = findViewById(R.id.tp_date);timePicker.setIs24HourView(true);textView = findViewById(R.id.tv_date);}@Overridepublic void onClick(View v) {if (v.getId() ==R.id.btn_time ){String desc2 = String.format("您选择的时间是 %d:%d", timePicker.getHour(), timePicker.getMinute() );textView.setText(desc2);}else {//构建一个时间对话框,该对话框已经集成了时间选择器TimePickerDialog timePickerDialog = new TimePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT,this, 21, 12, true);timePickerDialog.show();}}@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) {String desc2 = String.format("您选择的时间是 %d:%d", hourOfDay, minute );textView.setText(desc2);}
}