android笔记05_中既控件
中级控件
目录
1.图形定制
图形Drawable
形状图形
点9图片
状态列表图片
2.选择按钮
自定义按钮点击背景
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_process"/> <item android:drawable="@drawable/button_normal"/> </selector>
复选框CheckBox
开关按钮Switch
单选按钮RadioButton
<RadioGroup android:id="@+id/rg_gender" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_male" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="男"/> <RadioButton android:id="@+id/rb_female" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="女"/> </RadioGroup>
3.文本输入
编辑框EditText
焦点变更监听器
文本变化监听器
4.对话框
提醒对话框
日期对话框
时间对话框
5.项目实战:登录案例
login.xml
<?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"> <RadioGroup android:id="@+id/rg_login" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:checked="true" android:text="密码登录" /> <RadioButton android:id="@+id/rb_varify" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="验证码登录" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入手机号:" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <EditText android:id="@+id/et_phone" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/edittext_selector" android:hint="请输入手机号" android:inputType="number" android:maxLength="11"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="50dp" android:text="登陆密码:" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入密码" android:inputType="numberPassword" android:maxLength="11" /> <Button android:id="@+id/btn_forget" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="忘记密码" android:layout_alignParentEnd="true"/> </RelativeLayout> </LinearLayout> <CheckBox android:id="@+id/ck_rember" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="记住密码"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> </LinearLayout>
LoginActivity.java
package com.example.chapter05_middlecontrol; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.example.chapter05_middlecontrol.util.ViewUtil; import java.util.Random; public class LoginMainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { private TextView tv_password; private EditText et_password; private Button btn_forget; private CheckBox ck_rember; private EditText et_phone; private RadioButton rb_password; private RadioButton rb_varify; private ActivityResultLauncher<Intent> register; private Button btn_login; private String mPass = "123456"; private String varifycode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_main); et_phone = findViewById(R.id.et_phone); tv_password = findViewById(R.id.tv_password); et_password = findViewById(R.id.et_password); btn_forget = findViewById(R.id.btn_forget); ck_rember = findViewById(R.id.ck_rember); rb_password = findViewById(R.id.rb_password); rb_varify = findViewById(R.id.rb_varify); btn_login = findViewById(R.id.btn_login); RadioGroup rg_login = findViewById(R.id.rg_login); rg_login.setOnCheckedChangeListener(this); btn_login.setOnClickListener(this); et_phone.addTextChangedListener(new HideTextWatcher(et_phone, 11)); et_password.addTextChangedListener(new HideTextWatcher(et_password, 6)); btn_forget.setOnClickListener(this); register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { Intent intent = result.getData(); if (intent!= null && result.getResultCode() == Activity.RESULT_OK){ mPass = intent.getStringExtra("new_pass"); } } }); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.rb_password: tv_password.setText("密码:"); et_password.setHint("请输入密码"); btn_forget.setText("忘记密码"); ck_rember.setVisibility(View.VISIBLE); break; case R.id.rb_varify: tv_password.setText("验证码:"); et_password.setHint("请输入验证码"); btn_forget.setText("获取验证码"); ck_rember.setVisibility(View.GONE); break; } } @Override public void onClick(View view) { String phone = et_phone.getText().toString(); if (phone.length() < 11) { Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_LONG).show(); System.out.println(phone); return; } switch (view.getId()) { case R.id.btn_forget: if (rb_password.isChecked()) { Intent intent = new Intent(this, LoginForgetActivity.class); intent.putExtra("phone", phone); register.launch(intent); } else if (rb_varify.isChecked()) { varifycode = String.format("%06d", new Random().nextInt(999999)); System.out.println(varifycode); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请记住验证码"); builder.setMessage("手机号" + phone + "验证码位" + varifycode); builder.setPositiveButton("好的", null); AlertDialog dialog = builder.create(); dialog.show(); } break; case R.id.btn_login: if (rb_password.isChecked()) { if (!mPass.equals(et_password.getText().toString())) { Toast.makeText(this, "请输入正确密码", Toast.LENGTH_SHORT).show(); System.out.println("请输入正确密码"); return; } loginSuccess(); } else if (rb_varify.isChecked()) { if (!varifycode.equals(et_password.getText().toString())) { Toast.makeText(this, "请输入正确验证码", Toast.LENGTH_SHORT).show(); System.out.println("请输入正确验证码"); return; } loginSuccess(); } break; } } private void loginSuccess() { String desc = String.format("您的手机号:%s,点击确定返回", et_phone.getText().toString()); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("登录成功"); builder.setMessage(desc); builder.setPositiveButton("确定返回", (dialogInterface, witch) -> { finish(); }); builder.setNegativeButton("我再看看", null); AlertDialog dialog = builder.create(); dialog.show(); System.out.println("登录陈工"); } private class HideTextWatcher implements TextWatcher { private EditText mView; private Integer mMaxLength; public HideTextWatcher(EditText et, int maxLength) { this.mView = et; this.mMaxLength = maxLength; } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.toString().length() == mMaxLength) { ViewUtil.hideOneInputMethod(LoginMainActivity.this, mView); } } } }
forget.xml
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入新密码:" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <EditText android:id="@+id/et_pass_first" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/edittext_selector" android:hint="请输入新密码" android:inputType="numberPassword" android:maxLength="6"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:text="确认密码:" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <EditText android:id="@+id/et_pass_second" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请确认密码" android:inputType="numberPassword" android:maxLength="11" /> </RelativeLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:text="验证码:" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <EditText android:id="@+id/et_varify" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入验证码" android:inputType="numberPassword" android:maxLength="11" /> <Button android:id="@+id/btn_varify" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="获取验证码" android:layout_alignParentEnd="true"/> </RelativeLayout> </LinearLayout> <Button android:id="@+id/btn_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定"/> </LinearLayout>
ForgetActivity.java
package com.example.chapter05_middlecontrol; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import java.util.Random; public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener { private String mphone; private String varifycode; private EditText et_pass_first; private EditText et_pass_second; private EditText et_varify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_forget); mphone = getIntent().getStringExtra("phone"); findViewById(R.id.btn_varify).setOnClickListener(this); findViewById(R.id.btn_confirm).setOnClickListener(this); et_pass_first = findViewById(R.id.et_pass_first); et_pass_second = findViewById(R.id.et_pass_second); et_varify = findViewById(R.id.et_varify); } @Override public void onClick(View view) { switch(view.getId()){ case R.id.btn_varify: varifycode = String.format("%06d", new Random().nextInt(999999)); System.out.println(varifycode); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请记住验证码"); builder.setMessage("手机号" + mphone + "验证码位" + varifycode); builder.setPositiveButton("好的", null); AlertDialog dialog = builder.create(); dialog.show(); break; case R.id.btn_confirm: String first = et_pass_first.getText().toString(); String second = et_pass_second.getText().toString(); if ( first.length() < 6){ System.out.println("请输入正确的密码"); return ; } if ( !first.equals(second)){ System.out.println("密码不一致"); return; } if (!varifycode.equals(et_varify.getText().toString())){ System.out.println("验证码不对"); return; } System.out.println("修改成功"); Intent intent = new Intent(); intent.putExtra("new_pass", first); setResult(Activity.RESULT_OK, intent); finish(); break; } } }
本文作者:lmyyyy
本文链接:https://www.cnblogs.com/lmyy/p/17131678.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步