直播系统搭建,点击显示或隐藏密码
在前端中我们知道用javascript就可以可以很容易实现,那么在Android中怎么实现这个功能呢?
Java代码:
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText password;
private ImageView imageView;
private boolean flag=true;
private HideReturnsTransformationMethod method_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
password = findViewById(R.id.password);
imageView = findViewById(R.id.img);
imageView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.img:
changeImage();
}
}
private void changeImage() {
// todo 每次进入登陆时输入的密码都是隐藏的
PasswordTransformationMethod methodHide=PasswordTransformationMethod.getInstance();
password.setTransformationMethod(methodHide);
if(flag){
imageView.setImageDrawable(this.getResources().getDrawable(R.drawable.eye1));
flag=false;
// todo 隐藏密码
PasswordTransformationMethod method_hide = PasswordTransformationMethod.getInstance();
password.setTransformationMethod(method_hide);
}
else{
imageView.setImageDrawable(this.getResources().getDrawable(R.drawable.eye2));
flag=true;
// todo 显示密码
method_show= HideReturnsTransformationMethod.getInstance();
password.setTransformationMethod(method_show);
}
if (!password.getText().toString().trim().equals("")) {
password.setSelection(password.getText().toString().trim().length());//将光标移至文字末尾
}
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal" >
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:inputType="textPassword"
android:hint="请设置登录密码"
android:background="@null"/>
<button
android:id="@+id/btn"
android:layout_width="89dp"
android:layout_height="match_parent"
android:background="@drawable/eye2"/>
</LinearLayout>
</LinearLayout>
以上就是直播系统搭建,点击显示或隐藏密码, 更多内容欢迎关注之后的文章
标签:layout,id,点击,直播,import,android,password,method,搭建 From: https://www.cnblogs.com/yunbaomengnan/p/16591391.html