android app自动更新界面_Android自定义view之模仿登录界面文本输入框(华为云APP)...
生活随笔
收集整理的這篇文章主要介紹了
android app自动更新界面_Android自定义view之模仿登录界面文本输入框(华为云APP)...
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
好久不見!!!!!,最近終于擠出時(shí)間來(lái)更新文章了,廢話不多說(shuō),直接開始。
效果圖如下:
01
分析
1.組合多個(gè)控件完成此輸入框靜態(tài)效果
2.hint值上浮下潛動(dòng)畫
3.一些功能
02
步驟
01
自定義一個(gè)控件
public class MyEditVIew extends RelativeLayout { public MyEditVIew(Context context) { super(context,null); } public MyEditVIew(Context context, AttributeSet attrs) { super(context, attrs,0); } public MyEditVIew(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}02
寫一個(gè)相似布局(代碼在最后)
03
將布局打氣到View中
?LayoutInflater.from(context).inflate(R.layout.my_edit_view,?this);04
小提示文字上浮下潛動(dòng)畫
//小提示文字出現(xiàn)動(dòng)畫 private void minTextshow(TextView textView) { AnimationSet animationSet = new AnimationSet(true); Animation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); Animation alphaAnimation = new AlphaAnimation(0, 1f); animationSet.addAnimation(mHiddenAction); animationSet.addAnimation(alphaAnimation); animationSet.setDuration(300); textview.startAnimation(animationSet); } //小提示文字隱藏動(dòng)畫 private void minTexthide(TextView textView) { AnimationSet animationSet = new AnimationSet(true); Animation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); mShowAction.setRepeatMode(Animation.REVERSE); Animation alphaAnimation = new AlphaAnimation(1f, 0); animationSet.addAnimation(mShowAction); animationSet.addAnimation(alphaAnimation); animationSet.setRepeatMode(Animation.REVERSE); animationSet.setDuration(300); textview.startAnimation(animationSet); CountDownTimer countDownTimer = new CountDownTimer(300, 300) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { textview.setText(""); } }.start(); }05
密碼加密解密顯示
//設(shè)置文字非加密 HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance(); edittext.setTransformationMethod(method); //設(shè)置文字加密 TransformationMethod method = PasswordTransformationMethod.getInstance();?edittext.setTransformationMethod(method);06
其他一些小知識(shí)點(diǎn)
1.將光標(biāo)移到最后//將光標(biāo)移到最后edittext.setSelection(edittext.getText().toString().length());2.將鍵盤中的回車和空格去除 public static void setEditTextInputSpace(EditText editText) { InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (source.equals(" ") || source.toString().contentEquals("\n")) { return ""; } else { return null; } } }; editText.setFilters(new InputFilter[]{filter});????}3.給自定義view對(duì)外提供一個(gè)獲取值的方法 public String getText() { return edittext.getText().toString();????}07
源碼
1.MyEditVIew.java
public class MyEditVIew extends RelativeLayout { private TextView textview; private EditText edittext; private boolean mtextisshow; //文字是否顯示判斷 private boolean imgisshow; //圖片是否顯示判斷 private String hintText; private ImageView imageView; private ImageView iV_clean; public MyEditVIew(Context context) { super(context,null); } public MyEditVIew(Context context, AttributeSet attrs) { super(context, attrs,0); init(context, attrs); setEditTextInputSpace(edittext); textAddChanged(); imageOnClick(); } public MyEditVIew(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //打氣布局,獲取自定義屬性的值 private void init(Context context, AttributeSet attrs) { LayoutInflater.from(context).inflate(R.layout.my_edit_view, this); textview = findViewById(R.id.textview); edittext = findViewById(R.id.edittext); imageView = findViewById(R.id.imageView); iV_clean=findViewById(R.id.iV_clean); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyEditVIew); hintText = ta.getString(R.styleable.MyEditVIew_myhintText); } //文字輸入監(jiān)聽以及一些邏輯處理(未優(yōu)化) private void textAddChanged(){ edittext.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { int textSum = s.toString().trim().length(); if (textSum == 0) { if (mtextisshow == true) { minTexthide(textview); mtextisshow = false; iV_clean.setVisibility(INVISIBLE); edittext.setHint(hintText); } } else { if (imgisshow) { //設(shè)置文字非加密 HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance(); edittext.setTransformationMethod(method); edittext.setSelection(edittext.getText().toString().length()); } else { //設(shè)置文字加密 TransformationMethod method = PasswordTransformationMethod.getInstance(); edittext.setTransformationMethod(method); //將光標(biāo)移到最后 edittext.setSelection(edittext.getText().toString().length()); } if (mtextisshow == false) { textview.setText(hintText); minTextshow(textview); iV_clean.setVisibility(VISIBLE); mtextisshow = true; } } } }); } //兩個(gè)圖片的點(diǎn)擊事件,加密,清除文字 private void imageOnClick(){ imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (imgisshow) { imageView.setImageResource(R.mipmap.password_show); HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance(); edittext.setTransformationMethod(method); edittext.setSelection(edittext.getText().toString().length()); imgisshow = false; } else { imageView.setImageResource(R.mipmap.pwd_invisible); TransformationMethod method = PasswordTransformationMethod.getInstance(); edittext.setTransformationMethod(method); edittext.setSelection(edittext.getText().toString().length()); imgisshow = true; } } }); iV_clean.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { edittext.setText(""); } }); } //小提示文字出現(xiàn)動(dòng)畫 private void minTextshow(TextView textView) { AnimationSet animationSet = new AnimationSet(true); Animation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); Animation alphaAnimation = new AlphaAnimation(0, 1f); animationSet.addAnimation(mHiddenAction); animationSet.addAnimation(alphaAnimation); animationSet.setDuration(300); textview.startAnimation(animationSet); } //小提示文字隱藏動(dòng)畫 private void minTexthide(TextView textView) { AnimationSet animationSet = new AnimationSet(true); Animation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); mShowAction.setRepeatMode(Animation.REVERSE); Animation alphaAnimation = new AlphaAnimation(1f, 0); animationSet.addAnimation(mShowAction); animationSet.addAnimation(alphaAnimation); animationSet.setRepeatMode(Animation.REVERSE); animationSet.setDuration(300); textview.startAnimation(animationSet); CountDownTimer countDownTimer = new CountDownTimer(300, 300) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { textview.setText(""); } }.start(); } //將鍵盤中的回車和空格去除 public static void setEditTextInputSpace(EditText editText) { InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (source.equals(" ") || source.toString().contentEquals("\n")) { return ""; } else { return null; } } }; editText.setFilters(new InputFilter[]{filter}); } //提供一個(gè)可獲取的值 public String getText() { return edittext.getText().toString(); }}2.my_edit_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="4dp" android:paddingRight="4dp" > <TextView android:id="@+id/textview" android:textSize="12sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#b1b1b1" android:layout_marginRight="16dp" android:layout_marginLeft="16dp"/><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/edittext" android:layout_weight="1" android:layout_height="wrap_content" android:background="@null" android:hint="密碼" android:textSize="22sp" android:layout_width="0dp" android:layout_marginLeft="16dp" android:layout_marginBottom="6dp" android:lines="1" /> <ImageView android:id="@+id/iV_clean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/clean" android:visibility="invisible" android:layout_marginRight="4dp"/> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/pwd_invisible" android:layout_marginRight="16dp"/> LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#b1b1b1" android:layout_marginRight="16dp" android:layout_marginLeft="16dp"/>LinearLayout>3.attrs文件
<declare-styleable name="MyEditVIew"> <attr name="myhintText" format="string"/> declare-styleable>希望對(duì)你有所幫助,我們下一篇文章見!
微信公眾號(hào)|計(jì)蒙不吃魚
OSCHINA?|?計(jì)蒙不吃魚
新浪微博|計(jì)蒙不吃魚
CSDN?|?計(jì)蒙不吃魚
總結(jié)
以上是生活随笔為你收集整理的android app自动更新界面_Android自定义view之模仿登录界面文本输入框(华为云APP)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 吃鸡躲猫猫模式在哪?
- 下一篇: 安卓java音乐播放器下一曲_Andro