• 对话框


    1.焦点变更监听器

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. tools:context=".EditFocusActivity"
    9. android:padding="5dp">
    10. <EditText
    11. android:id="@+id/et_phone"
    12. android:layout_width="match_parent"
    13. android:layout_height="wrap_content"
    14. android:hint="请输入11位手机号码"
    15. android:inputType="text"
    16. android:maxLength="11"
    17. android:padding="6dp"
    18. android:layout_margin="5dp"
    19. android:background="@drawable/edit_select"/>
    20. <EditText
    21. android:id="@+id/et_password"
    22. android:layout_width="match_parent"
    23. android:layout_height="wrap_content"
    24. android:hint="请输入密码"
    25. android:padding="6dp"
    26. android:inputType="numberPassword"
    27. android:layout_margin="5dp"
    28. android:background="@drawable/edit_select"
    29. android:maxLength="11"/>
    30. <Button
    31. android:id="@+id/btn_login"
    32. android:layout_width="match_parent"
    33. android:layout_margin="5dp"
    34. android:layout_height="wrap_content"
    35. android:text="登录"/>
    36. LinearLayout>
    1. package com.tiger.chapter05;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.EditText;
    6. import android.widget.Toast;
    7. public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {
    8. private EditText et_phone;
    9. private EditText et_password;
    10. @Override
    11. protected void onCreate(Bundle savedInstanceState) {
    12. super.onCreate(savedInstanceState);
    13. setContentView(R.layout.activity_edit_focus);
    14. et_phone = findViewById(R.id.et_phone);
    15. et_password = findViewById(R.id.et_password);
    16. et_password.setOnFocusChangeListener(this);
    17. }
    18. @Override
    19. public void onFocusChange(View v, boolean hasFocus) {
    20. if (hasFocus){
    21. String phone = et_phone.getText().toString();
    22. if (phone==null||phone.length()<11){
    23. //手机号码编辑框请求焦点,也就是把光标移回手机号码编辑框
    24. et_phone.requestFocus();
    25. Toast.makeText(this,"请输入11位手机号码",Toast.LENGTH_SHORT).show();//short短时间小时
    26. }
    27. }
    28. }
    29. }

    2.文本变化监听器

     

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. android:padding="5dp"
    9. tools:context=".EditHideActivity">
    10. <EditText
    11. android:id="@+id/et_phone"
    12. android:layout_width="match_parent"
    13. android:layout_height="wrap_content"
    14. android:hint="输入11位时自动隐藏输入法"
    15. android:layout_margin="5dp"
    16. android:padding="5dp"
    17. android:background="@drawable/edit_select"
    18. android:inputType="text"
    19. android:maxLength="11"
    20. />
    21. <EditText
    22. android:id="@+id/et_password"
    23. android:layout_width="match_parent"
    24. android:layout_height="wrap_content"
    25. android:hint="输入6位自动隐藏输入法"
    26. android:layout_margin="5dp"
    27. android:padding="5dp"
    28. android:inputType="textPassword"
    29. android:background="@drawable/edit_select"
    30. android:maxLength="6"
    31. />
    32. LinearLayout>
    1. package com.tiger.chapter05.util;
    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.view.View;
    5. import android.view.inputmethod.InputMethodManager;
    6. public class ViewUtil {
    7. public static void hideOneInputMethod(Activity act, View view){
    8. //从系统服务中获取输入法管理器
    9. InputMethodManager manager = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
    10. //关闭屏幕上的输入法软键盘
    11. manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    12. }
    13. }
    1. package com.tiger.chapter05;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.text.Editable;
    5. import android.text.TextWatcher;
    6. import android.view.View;
    7. import android.widget.EditText;
    8. import android.widget.ImageButton;
    9. import com.tiger.chapter05.util.ViewUtil;
    10. public class EditHideActivity extends AppCompatActivity {
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_edit_hide);
    15. EditText et_phone = findViewById(R.id.et_phone);
    16. EditText et_password = findViewById(R.id.et_password);
    17. et_phone.addTextChangedListener(new HideTextWatcher(et_phone,11));
    18. et_password.addTextChangedListener(new HideTextWatcher(et_password,6));
    19. }
    20. //内存泄漏
    21. private class HideTextWatcher implements TextWatcher {
    22. private EditText mView;
    23. private Integer maxLength;
    24. public HideTextWatcher(EditText mView, Integer maxLength) {
    25. this.mView = mView;
    26. this.maxLength = maxLength;
    27. }
    28. @Override
    29. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    30. }
    31. @Override
    32. public void onTextChanged(CharSequence s, int start, int before, int count) {
    33. }
    34. @Override
    35. public void afterTextChanged(Editable s) {
    36. //获得已输入的文本字符串
    37. String str = s.toString();
    38. //输入文本达到11位 (如手机号码),或者达到6位 (如登录密码)时 关闭输入法
    39. if (str.length()== maxLength){
    40. ViewUtil.hideOneInputMethod(EditHideActivity.this,mView);
    41. }
    42. }
    43. }
    44. }

    3. 提醒对话框

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. android:padding="5dp"
    9. tools:context=".CheckBoxActivity">
    10. <Button
    11. android:id="@+id/btn_alert"
    12. android:layout_width="match_parent"
    13. android:layout_height="wrap_content"
    14. android:text="弹出提醒对话框" />
    15. <TextView
    16. android:id="@+id/tv_alert"
    17. android:layout_width="match_parent"
    18. android:layout_height="wrap_content"
    19. android:layout_marginTop="5dp" />
    20. LinearLayout>

     

    1. package com.tiger.chapter05;
    2. import androidx.appcompat.app.AlertDialog;
    3. import androidx.appcompat.app.AppCompatActivity;
    4. import android.content.DialogInterface;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.Button;
    8. import android.widget.TextView;
    9. public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
    10. private TextView tv_alert;
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_alert_dialog);
    15. findViewById(R.id.btn_alert).setOnClickListener(this);
    16. tv_alert = findViewById(R.id.tv_alert);
    17. }
    18. @Override
    19. public void onClick(View v) {
    20. //创建提醒对话框的建造器
    21. //这个Builder是个静态内部类
    22. AlertDialog.Builder builder = new AlertDialog.Builder(this)//将上下文放进去
    23. .setTitle("尊敬的用户")//设置对话框的标题文本
    24. .setMessage("你真的要卸载我吗?")//设置对话框的内容文本
    25. .setPositiveButton("残忍卸载", new DialogInterface.OnClickListener() {
    26. @Override
    27. public void onClick(DialogInterface dialog, int which) {
    28. tv_alert.setText("虽然依依不舍,但是只能离开了");
    29. }
    30. })//设置对话框的肯定按钮文本及其点击监听器
    31. .setNegativeButton("我再想想", new DialogInterface.OnClickListener() {
    32. @Override
    33. public void onClick(DialogInterface dialog, int which) {
    34. tv_alert.setText("让我再陪你三百六十五个日夜");
    35. }
    36. });//设置对话框的否定按钮文本及其点击监听器
    37. //根据建造起创建提醒对话框对象
    38. AlertDialog alertDialog = builder.create();
    39. //显示提醒对话框
    40. alertDialog.show();
    41. }
    42. }

    4.日期对话框

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. android:padding="5dp"
    9. tools:context=".DatePickerActivity">
    10. <ScrollView
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content">
    13. <LinearLayout
    14. android:layout_width="match_parent"
    15. android:layout_height="wrap_content"
    16. android:orientation="vertical">
    17. <DatePicker
    18. android:id="@+id/dp_date1"
    19. android:layout_width="match_parent"
    20. android:layout_height="wrap_content"
    21. android:datePickerMode="spinner"
    22. android:calendarViewShown="false"/>
    23. <LinearLayout
    24. android:layout_width="match_parent"
    25. android:layout_height="wrap_content"
    26. android:gravity="center">
    27. <DatePicker
    28. android:id="@+id/dp_date2"
    29. android:layout_width="wrap_content"
    30. android:layout_height="wrap_content"
    31. android:datePickerMode="calendar"
    32. />
    33. LinearLayout>
    34. <Button
    35. android:id="@+id/btn_ok"
    36. android:layout_width="match_parent"
    37. android:layout_height="wrap_content"
    38. android:text="确定" />
    39. <TextView
    40. android:id="@+id/tv_date"
    41. android:layout_width="match_parent"
    42. android:layout_height="wrap_content"/>
    43. <Button
    44. android:id="@+id/btn_date"
    45. android:layout_width="match_parent"
    46. android:layout_height="wrap_content"
    47. android:text="弹出日期对话框" />
    48. LinearLayout>
    49. ScrollView>
    50. LinearLayout>

     

    1. package com.tiger.chapter05;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.DatePicker;
    7. import android.widget.TextView;
    8. public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener {
    9. private DatePicker dp_date1;
    10. private DatePicker dp_date2;
    11. private TextView viewById;
    12. private Button button;
    13. @Override
    14. protected void onCreate(Bundle savedInstanceState) {
    15. super.onCreate(savedInstanceState);
    16. setContentView(R.layout.activity_date_picker);
    17. findViewById(R.id.btn_ok).setOnClickListener(this);
    18. dp_date1 = findViewById(R.id.dp_date1);
    19. dp_date2 = findViewById(R.id.dp_date2);
    20. viewById = findViewById(R.id.tv_date);
    21. button = findViewById(R.id.btn_date);
    22. }
    23. @Override
    24. public void onClick(View v) {
    25. if (v.getId() == R.id.btn_ok) {
    26. //月份 是0开始 所以加一
    27. String desc = String.format("您选择的日期是 %d年%d月%d日", dp_date1.getYear(), dp_date1.getMonth()+1, dp_date1.getDayOfMonth())+"\n";
    28. String desc2 = String.format("您选择的日期是 %d年%d月%d日", dp_date2.getYear(), dp_date2.getMonth()+1, dp_date2.getDayOfMonth());
    29. String result = desc+desc2;
    30. viewById.setText(result);
    31. }else {
    32. }
    33. }
    34. }

    1. package com.tiger.chapter05;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.app.DatePickerDialog;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.Button;
    7. import android.widget.DatePicker;
    8. import android.widget.TextView;
    9. import java.util.Calendar;
    10. public class DatePickerActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
    11. private DatePicker dp_date1;
    12. private DatePicker dp_date2;
    13. private TextView viewById;
    14. private Button button;
    15. @Override
    16. protected void onCreate(Bundle savedInstanceState) {
    17. super.onCreate(savedInstanceState);
    18. setContentView(R.layout.activity_date_picker);
    19. findViewById(R.id.btn_ok).setOnClickListener(this);
    20. dp_date1 = findViewById(R.id.dp_date1);
    21. dp_date2 = findViewById(R.id.dp_date2);
    22. viewById = findViewById(R.id.tv_date);
    23. button = findViewById(R.id.btn_date);
    24. button.setOnClickListener(this);
    25. }
    26. @Override
    27. public void onClick(View v) {
    28. if (v.getId() == R.id.btn_ok) {
    29. //月份 是0开始 所以加一
    30. String desc = String.format("您选择的日期是 %d年%d月%d日", dp_date1.getYear(), dp_date1.getMonth() + 1, dp_date1.getDayOfMonth()) + "\n";
    31. String desc2 = String.format("您选择的日期是 %d年%d月%d日", dp_date2.getYear(), dp_date2.getMonth() + 1, dp_date2.getDayOfMonth());
    32. String result = desc + desc2;
    33. viewById.setText(result);
    34. } else {
    35. //获取日历的一个实例 ,里面包含了当前的年月日
    36. Calendar instance = Calendar.getInstance();
    37. int year = instance.get(Calendar.YEAR);
    38. int month = instance.get(Calendar.MONTH);
    39. int day = instance.get(Calendar.DATE);
    40. //不用减1
    41. DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);
    42. //显示日期对话框
    43. datePickerDialog.show();
    44. }
    45. }
    46. @Override
    47. public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    48. String result = String.format("您选择的日期是 %d年%d月%d日", year, month + 1, dayOfMonth);
    49. viewById.setText(result);
    50. }
    51. }
    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. android:padding="5dp"
    9. tools:context=".DatePickerActivity">
    10. <ScrollView
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content">
    13. <LinearLayout
    14. android:layout_width="match_parent"
    15. android:layout_height="wrap_content"
    16. android:orientation="vertical">
    17. <DatePicker
    18. android:id="@+id/dp_date1"
    19. android:layout_width="match_parent"
    20. android:layout_height="wrap_content"
    21. android:calendarViewShown="false"
    22. android:datePickerMode="spinner" />
    23. <LinearLayout
    24. android:layout_width="match_parent"
    25. android:layout_height="wrap_content"
    26. android:gravity="center">
    27. <DatePicker
    28. android:id="@+id/dp_date2"
    29. android:layout_width="wrap_content"
    30. android:layout_height="wrap_content"
    31. android:datePickerMode="calendar" />
    32. LinearLayout>
    33. <Button
    34. android:id="@+id/btn_ok"
    35. android:layout_width="match_parent"
    36. android:layout_height="wrap_content"
    37. android:text="确定" />
    38. <TextView
    39. android:id="@+id/tv_date"
    40. android:layout_width="match_parent"
    41. android:layout_height="wrap_content" />
    42. <Button
    43. android:id="@+id/btn_date"
    44. android:layout_width="match_parent"
    45. android:layout_height="wrap_content"
    46. android:text="弹出日期对话框" />
    47. LinearLayout>
    48. ScrollView>
    49. LinearLayout>

    TimePicker&TimePickerDialog

    TimePicker 有两种 clock  spinner

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical"
    8. tools:context=".TimePickerActivity">
    9. <Button
    10. android:id="@+id/btn_time"
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content"
    13. android:text="请选择时间" />
    14. <TimePicker
    15. android:id="@+id/tp_date"
    16. android:layout_width="match_parent"
    17. android:layout_height="wrap_content"
    18. android:timePickerMode="spinner" />
    19. <TextView
    20. android:id="@+id/tv_date"
    21. android:layout_width="match_parent"
    22. android:layout_height="wrap_content" />
    23. <Button
    24. android:id="@+id/btn_d"
    25. android:layout_width="match_parent"
    26. android:layout_height="wrap_content"
    27. android:text="时间对话框" />
    28. LinearLayout>
    1. package com.tiger.chapter05;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.app.AlertDialog;
    4. import android.app.TimePickerDialog;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.TextView;
    8. import android.widget.TimePicker;
    9. public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {
    10. private TimePicker timePicker;
    11. private TextView textView;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.activity_time_picker);
    16. findViewById(R.id.btn_time).setOnClickListener(this);
    17. findViewById(R.id.btn_d).setOnClickListener(this);
    18. timePicker = findViewById(R.id.tp_date);
    19. timePicker.setIs24HourView(true);
    20. textView = findViewById(R.id.tv_date);
    21. }
    22. @Override
    23. public void onClick(View v) {
    24. if (v.getId() ==R.id.btn_time ){
    25. String desc2 = String.format("您选择的时间是 %d:%d", timePicker.getHour(), timePicker.getMinute() );
    26. textView.setText(desc2);
    27. }else {
    28. //构建一个时间对话框,该对话框已经集成了时间选择器
    29. TimePickerDialog timePickerDialog = new TimePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT,this, 21, 12, true);
    30. timePickerDialog.show();
    31. }
    32. }
    33. @Override
    34. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    35. String desc2 = String.format("您选择的时间是 %d:%d", hourOfDay, minute );
    36. textView.setText(desc2);
    37. }
    38. }

  • 相关阅读:
    Django项目无法安装python-ldap依赖解决方案
    Spring --- AOP 操作 - AspectJ注解
    深度神经网络的工作原理,深度神经网络工作原理
    Java 内部类 面试“变态题”
    Docker实战-部署GPE微服务的监控体系(二)
    Win7下设置“定时关机”的方法
    DNS域名解析过程剖析
    开源IDaaS方舟一账通ArkID系统内置OIDC 认证插件配置流程
    cmdline-tools component is missing
    【Python百日进阶-数据分析】Day325 - plotly.express.scatter_3d():3D散点图
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/136456908