温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么通过自定义EditText实现淘宝登录功能

发布时间:2021-06-28 17:43:30 来源:亿速云 阅读:145 作者:Leah 栏目:移动开发

本篇文章为大家展示了Android中怎么通过自定义EditText实现淘宝登录功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

整体布局UI:

 <com.example.zdyedittext.ClearEditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:layout_alignTop="@+id/imageView1"
    android:layout_marginLeft="17dp"
    android:layout_toRightOf="@+id/imageView1"
    android:background="@android:color/white"
    android:ems="10"
    android:hint="手机号"
    android:padding="8dp"
    android:singleLine="true" />

  <com.example.zdyedittext.ClearEditText
    android:id="@+id/et_pass_word"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密码"
    android:background="@android:color/white"
    android:password="true"
    android:padding="8dp"
    android:singleLine="true" />

自定义EditText类

由于自定义EditText理所当然要集成EditText

public class ClearEditText extends EditText

然后添加构造方法,是为了能在XML中能够引用。

 public ClearEditText(Context context, AttributeSet attrs) {  
    this(context, attrs, android.R.attr.editTextStyle); 
  }

接下来就是设置自己的EditText的样式,添加自己想要的样式。具体是在init()方法中实现。

 public ClearEditText(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
  }

init()方法的实现过程:[2]参数为:dr.mDrawableRight,定义删除按钮是在EditText的右边,设置图标的左上右下:mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());

private void init() { 
    // 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片 
    mClearDrawable = getCompoundDrawables()[2]; 
    if (mClearDrawable == null) {  
      mClearDrawable = getResources().getDrawable(R.drawable.del);//R.drawable.del删除图标的图片 
    } 
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); 

    //设置图标的左上右下
    // 默认设置隐藏图标 
    setClearIconVisible(false); 
    // 设置焦点改变的监听 
    setOnFocusChangeListener(this); 
    // 设置输入框里面内容发生改变的监听 
    addTextChangedListener(this); 
  }

由于不能直接给EditText设置监听事件,所以采用记录点击位置来模拟点击事件,只记录了鱼图标的左右点击。

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (getCompoundDrawables()[2] != null) { 

        boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight()))); 

        if (touchable) { 
          this.setText(""); 
        } 
      } 
    } 

    return super.onTouchEvent(event); 
  }


判断输入框中是否有文字,动态设置删除图标的显示和隐藏。

public void onFocusChange(View v, boolean hasFocus) { 
    this.hasFoucs = hasFocus; 
    if (hasFocus) { 
      setClearIconVisible(getText().length() > 0); 
    } else { 
      setClearIconVisible(false); 
    } 
  }

如果输入框中有文字 那么久绘制删除图标

protected void setClearIconVisible(boolean visible) { 
    Drawable right = visible ? mClearDrawable : null; 
    setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 
  }

 当输入框内容发生变化的时候动态改变删除图标

 public void onTextChanged(CharSequence s, int start, int count, int after) { 
    if (hasFoucs) { 
      setClearIconVisible(s.length() > 0); 
    } 
  }

至此就完成了:当属框中没有文本的时候 删除图标隐藏 当有文本输入的时候,删除图标显示,点击删除图标,清空文本内容。

自定义InputType返回为”*”

设置密码样式要继承PasswordTransformationMethod这个类然后实现CharSequence方法去修改CharAt的返回值为“*”即可。

 private class PasswordCharSequence implements CharSequence {
    private CharSequence mSource;
    public PasswordCharSequence(CharSequence source) {
      mSource = source; // Store char sequence
    }
    这里用于修改InputType的返回样式
    public char charAt(int index) {
      return '*'; // This is the important part
    }
    public int length() {
      return mSource.length(); // Return default
    }
    public CharSequence subSequence(int start, int end) {
      return mSource.subSequence(start, end); // Return default
    }
  }

然后在主程序中初始化控件,在布局中设置android:password=”true”这一行代码,以便在代码中动态设置密码输入的返回样式。

et_pass_word = (ClearEditText) findViewById(R.id.et_pass_word);
et_pass_word.setTransformationMethod(new EditTextBgToStar());

上述内容就是Android中怎么通过自定义EditText实现淘宝登录功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI