效果图:
只有当焦点在输入框中时才会d出键盘,当键盘是d出的时候,点击任何地方键盘都会关闭,
当输入密码的时候猫头鹰会将眼睛捂着,点击其他地方时便会将眼睛打开
GitHub地址:https://github.com/luofangli/MyCool_login
详细代码:
activity_main.xml中:
<?xml version="1.0" enCoding="utf-8"?>
<androIDx.constraintlayout.Widget.ConstraintLayout 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:background="@drawable/bg"
tools:context=".MainActivity">
<ImageVIEw
androID:ID="@+ID/head_image"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
app:layout_constraintBottom_totopOf="@+ID/guIDeline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/head" />
<ImageVIEw
androID:ID="@+ID/lefthand"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:src="@drawable/icon_hand"
app:layout_constraintBottom_totopOf="@+ID/guIDeline"
app:layout_constraintEnd_toStartOf="@+ID/head_image"
app:layout_constrainttop_totopOf="@+ID/guIDeline" />
<ImageVIEw
androID:ID="@+ID/righthand"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
app:layout_constraintBottom_totopOf="@+ID/guIDeline"
app:layout_constraintStart_toEndOf="@+ID/head_image"
app:layout_constrainttop_totopOf="@+ID/guIDeline"
app:srcCompat="@drawable/icon_hand" />
<ImageVIEw
androID:ID="@+ID/rightArm"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+ID/head_image"
app:layout_constrainttop_totopOf="@+ID/guIDeline"
app:srcCompat="@drawable/arm_right" />
<ImageVIEw
androID:ID="@+ID/leftArm"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@+ID/head_image"
app:layout_constrainttop_totopOf="@+ID/guIDeline"
app:srcCompat="@drawable/arm_left" />
<androIDx.constraintlayout.Widget.GuIDeline
androID:ID="@+ID/guIDeline"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:orIEntation="horizontal"
app:layout_constraintGuIDe_percent="0.25" />
<androIDx.constraintlayout.Widget.GuIDeline
androID:ID="@+ID/guIDeline2"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:orIEntation="horizontal"
app:layout_constraintGuIDe_percent="0.59" />
<ImageVIEw
androID:ID="@+ID/imageVIEw"
androID:layout_wIDth="0dp"
androID:layout_height="0dp"
androID:layout_marginStart="20dp"
androID:layout_marginEnd="20dp"
androID:scaleType="centerCrop"
app:layout_constraintBottom_totopOf="@+ID/guIDeline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constrainttop_totopOf="@+ID/guIDeline"
app:srcCompat="@drawable/bg_blure" />
<EditText
androID:ID="@+ID/edittext_name"
androID:layout_wIDth="0dp"
androID:layout_height="50dp"
androID:background="@drawable/shape_text"
androID:drawableStart="@drawable/iconFont_user"
androID:ems="10"
androID:hint="请输入账号"
androID:inputType="textPersonname"
androID:paddingStart="10dp"
app:layout_constraintBottom_totopOf="@+ID/edittext_password"
app:layout_constraintEnd_toEndOf="@+ID/edittext_password"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="@+ID/edittext_password"
app:layout_constrainttop_toBottomOf="@+ID/head_image" />
<EditText
androID:ID="@+ID/edittext_password"
androID:layout_wIDth="0dp"
androID:layout_height="50dp"
androID:layout_marginStart="40dp"
androID:layout_marginEnd="40dp"
androID:background="@drawable/shape_text"
androID:drawableStart="@drawable/iconFont_password"
androID:ems="10"
androID:hint="请输入密码"
androID:inputType="textPassword"
androID:maxLength="6"
androID:paddingStart="10dp"
app:layout_constraintBottom_totopOf="@+ID/guIDeline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constrainttop_toBottomOf="@+ID/edittext_name" />
<button
androID:ID="@+ID/button_login"
androID:layout_wIDth="0dp"
androID:layout_height="60dp"
androID:text="登录"
app:layout_constraintEnd_toEndOf="@+ID/imageVIEw"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+ID/imageVIEw"
app:layout_constrainttop_toBottomOf="@+ID/imageVIEw" />
</androIDx.constraintlayout.Widget.ConstraintLayout>
shape_text.xml中:
<?xml version="1.0" enCoding="utf-8"?>
<shape xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:shape="rectangle">
<corners androID:radius="20dp"/>
<stroke androID:color="@color/white"
androID:wIDth="1dp"/>
<solID androID:color="@color/lightGray"/>
</shape>
MainActivity中:
package com.example.my
import androID.animation.Animator
import androID.animation.AnimatorSet
import androID.animation.ObjectAnimator
import androID.content.Context
import androIDx.appcompat.app.AppCompatActivity
import androID.os.Bundle
import androID.text.Editable
import androID.text.TextWatcher
import androID.util.displayMetrics
import androID.util.Log
import androID.vIEw.MotionEvent
import androID.vIEw.VIEw
import androID.vIEw.inputmethod.inputMethodManager
import kotlinx.androID.synthetic.main.activity_main.*
import java.util.function.LongFunction
class MainActivity : AppCompatActivity(),TextWatcher,VIEw.OnFocuschangelistener {
//左边手臂
//升起
private val leftArmAnimatorSet:AnimatorSet by lazy {
val rotate=ObjectAnimator.offloat(leftArm,"rotation",
145f)
val transX = ObjectAnimator.offloat(leftArm,"translationX",
dp2px(50f))
val transY = ObjectAnimator.offloat(leftArm,"translationY",
dp2px(-45f))
AnimatorSet().apply {
duration = 500
playTogether(rotate,transX,transY)
}
}
//降落
private val leftArmAnimatorSetDown:AnimatorSet by lazy {
val rotate=ObjectAnimator.offloat(leftArm,"rotation",
0f)
val transX = ObjectAnimator.offloat(leftArm,"translationX",
dp2px(0f))
val transY = ObjectAnimator.offloat(leftArm,"translationY",
dp2px(0f))
AnimatorSet().apply {
duration = 500
playTogether(rotate,transX,transY)
}
}
//右边手臂
//升起
private val rightArmAnimator:AnimatorSet by lazy {
val rotate=ObjectAnimator.offloat(rightArm,"rotation",
-145f)
val transX = ObjectAnimator.offloat(rightArm,"translationX",
dp2px(-50f))
val transY = ObjectAnimator.offloat(rightArm,"translationY",
dp2px(-45f))
AnimatorSet().apply {
duration = 500
playTogether(rotate,transX,transY)
}
}
//降落
private val rightArmAnimatorDown:AnimatorSet by lazy {
val rotate=ObjectAnimator.offloat(rightArm,"rotation",
0f)
val transX = ObjectAnimator.offloat(rightArm,"translationX",
dp2px(0f))
val transY = ObjectAnimator.offloat(rightArm,"translationY",
dp2px(0f))
AnimatorSet().apply {
duration = 500
playTogether(rotate,transX,transY)
}
}
//左手掌手掌
private val lefttAnimatorhandUp:ObjectAnimator by lazy {
ObjectAnimator.offloat(lefthand,"translationY",dp2px(0f))
}
private val leftAnimatorhandDown:ObjectAnimator by lazy {
ObjectAnimator.offloat(lefthand,"translationY",dp2px(40f))
}
//右手掌
private val righttAnimatorhandUp:ObjectAnimator by lazy {
ObjectAnimator.offloat(righthand,"translationY",dp2px(0f))
}
private val rightAnimatorhandDown:ObjectAnimator by lazy {
ObjectAnimator.offloat(righthand,"translationY",dp2px(40f))
}
private var isopen = false
overrIDe fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentVIEw(R.layout.activity_main)
edittext_password.onFocuschangelistener = this
edittext_password.setonClickListener {
hIDeOropenBoard_vIEw(it)
}
edittext_name.setonClickListener {
hIDeOropenBoard_vIEw(it)
}
}
overrIDe fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
overrIDe fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
overrIDe fun afterTextChanged(s: Editable?) {
}
overrIDe fun ontouchEvent(event: MotionEvent?): Boolean {
when(event?.action){
MotionEvent.ACTION_DOWN->{
hIDeKeyboard()
}
}
return super.ontouchEvent(event)
}
//隐藏键盘
private fun hIDeKeyboard(){
val keyboard =getSystemService(Context.input_METHOD_SERVICE) as inputMethodManager
//this.currentFocus?.windowToken 是获取当前activity的windowToken
keyboard.hIDeSoftinputFromWindow(this.currentFocus?.windowToken,0)
}
//对特定视图键盘的调用
private fun hIDeOropenBoard_vIEw(v:VIEw){
val keyboard = getSystemService(Context.input_METHOD_SERVICE) as inputMethodManager
//keyboard.isActive是键盘的状态,若是打开的则返回true否则返回false
//
if (isopen){
Log.v("lfl","关闭键盘")
//强制关闭
keyboard.hIDeSoftinputFromWindow(v.windowToken,0)
isopen = !isopen
}else{
Log.v("lfl","打开键盘")
//打开
keyboard.showSoftinput(v,inputMethodManager.SHOW_FORCED)
isopen = !isopen
}
}
//若键盘是隐藏的则打开,若是打开的则隐藏
private fun hIDeOropenKeyboard(){
val keyboard = getSystemService(Context.input_METHOD_SERVICE) as inputMethodManager
keyboard.toggleSoftinput(0,inputMethodManager.HIDE_NOT_ALWAYS)
}
overrIDe fun onFocusChange(v: VIEw?, hasFocus: Boolean) {
if(v == edittext_password){
if (hasFocus){
//焦点在该视图上,蒙上眼睛
AnimatorSet().apply {
play(leftArmAnimatorSet)
.with(rightArmAnimator)
.after(leftAnimatorhandDown)
.after(rightAnimatorhandDown)
}.start()
}else{
//焦点没有在该视图上,打开眼睛
AnimatorSet().apply {
play(leftArmAnimatorSetDown)
.with(rightArmAnimatorDown)
.after(lefttAnimatorhandUp)
.after(righttAnimatorhandUp)
}.start()
}
}
}
//将dp值转换为相应的像素值
private fun dp2px(dp:float):float{
return resources.displayMetrics.density*dp
}
}
总结
以上是内存溢出为你收集整理的实例登录界面全部内容,希望文章能够帮你解决实例登录界面所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)