Android 笨办法实现editText动态输入框
2017-02-22 20:58
495 查看
自己在做app数字输入的时候,看到挖财记账的记一笔中的输入效果,感觉很是舒服,于是决定自己撸一个
先看看挖财的效果:
有两种方式可以实现:
1.自定义View来实现此控件,也就是重写下editText的代码,重写覆盖他的检测逻辑,来实现实时更新text。
2.通过添加新的textView,给editText添加textWeather监听输入,然后判断再根据实际需求来更新textView。
当然我这里为了快速出效果直接操作第二种:
布局
核心代码:(这里我的检测是这样,小数点前不管,小数点后两位)
代码是不是很简单,下面讲注意的几点:
1.editText所有效果都要没有或者说是和背景融为一体
2.textView放到原来editText显示的。
下面来看看效果:
效果基本都达到了,剩下的就是调整editText不显示,textView显示,以及textView的位置了,就不再说了。
附上调整后的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/fragment_account_book_num"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="@color/colorAccent"
android:textSize="0dp"
android:background="@null"
android:inputType="numberDecimal"
android:cursorVisible="false"
android:text=""/>
<TextView
android:id="@+id/test_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="0.00"
android:textColor="@color/colorAccent"
android:textAlignment="gravity"
android:gravity="center_vertical"
android:textSize="18dp"/>
</RelativeLayout>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
以及最终效果:
先看看挖财的效果:
有两种方式可以实现:
1.自定义View来实现此控件,也就是重写下editText的代码,重写覆盖他的检测逻辑,来实现实时更新text。
2.通过添加新的textView,给editText添加textWeather监听输入,然后判断再根据实际需求来更新textView。
当然我这里为了快速出效果直接操作第二种:
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="15dp"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp"> <android.support.design.widget.TextInputEditText android:id="@+id/fragment_account_book_num" android:layout_width="match_parent" android:layout_height="50dp" android:textColor="@color/colorAccent" android:background="@null" android:inputType="numberDecimal" android:hint="输入金额" android:cursorVisible="false" android:text=""/> <TextView android:id="@+id/test_text" android:layout_width="match_parent" android:layout_height="50dp" android:text="0.00" android:textColor="@color/colorAccent" android:layout_alignParentBottom="true"/> </RelativeLayout> </android.support.design.widget.TextInputLayout> </LinearLayout>
核心代码:(这里我的检测是这样,小数点前不管,小数点后两位)
rootView = inflater.inflate(R.layout.fragment_account_book,container,false); inputEditText = (TextInputEditText)rootView.findViewById(R.id.fragment_account_book_num); final TextView test_text = (TextView)rootView.findViewById(R.id.test_text); //添加文字改变监听 inputEditText.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){ String str = s.toString(); Log.i(TAG,"onTextChanged: text变化: "+str); if(str.contains(".")) { Log.i(TAG,"onTextChanged: 到了位置。"); String lastS = str.substring(str.indexOf(".")+1,str.length()); str = str.substring(0,str.indexOf(".")); switch(lastS.length()) { case 0: test_text.setText(str+".00"); break; case 1: test_text.setText(str+"."+lastS+"0"); break; case 2: test_text.setText(str+"."+lastS); break; default://超过两位,不变化,而且值保留两位,不再多保存,防止一直删除确看起来删除不了的bug str = str + "." + lastS.substring(0,2); inputEditText.setText(str); inputEditText.setSelection(str.length());//设置光标位置到最后,避免输入从头开始的bug break; } } else if(!str.trim().equals("")){ test_text.setText(str+".00"); } else { test_text.setText("0.00"); } } @Override public void afterTextChanged(Editable s){ } });
代码是不是很简单,下面讲注意的几点:
1.editText所有效果都要没有或者说是和背景融为一体
2.textView放到原来editText显示的。
下面来看看效果:
效果基本都达到了,剩下的就是调整editText不显示,textView显示,以及textView的位置了,就不再说了。
附上调整后的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/fragment_account_book_num"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="@color/colorAccent"
android:textSize="0dp"
android:background="@null"
android:inputType="numberDecimal"
android:cursorVisible="false"
android:text=""/>
<TextView
android:id="@+id/test_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="0.00"
android:textColor="@color/colorAccent"
android:textAlignment="gravity"
android:gravity="center_vertical"
android:textSize="18dp"/>
</RelativeLayout>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
以及最终效果:
相关文章推荐
- android输入框EditText输入银行卡,输入手机,输入身份证格式化的实现
- android 实现edittext的动态可编辑和不可编辑
- android中EditText点击右边叉叉按钮(或图片)清除输入框内的文字的实现
- android之EditText实现动态显示隐藏密码输入框的内容(实用)
- Android中EditText实现不可编辑解决办法
- Android中ClearEditText实现点击EditText输入框右边清除图标来清除输入内容的两种方式
- Android中ClearEditText实现点击EditText输入框右边清除图标来清除输入内容的两种方式
- Android UI设计之<三>自定义EditText,实现带清除功能的输入框
- Android打造带删除的EditText并且实现输入框密码显示、隐藏
- Android 动态显示隐藏EditText 输入框内容
- Android中EditText实现不可编辑解决办法
- Android EditText的输入监听,输入字符的动态获取
- Android中实现Edittext中输入表情图片的方法
- android edittext,listview里面显示gif动态表情
- Android开发:EditText和ListView或GridView同时使用,输入法自动跳出来的 解决办法
- Android字数限制的EditText实现方案研究
- android中画文字的换行 办法(对于遇到canvas.drawText(String s )无法实现换行问题的解决)
- android关于实现EditText中加多行下划线的的一种方法
- Android开发心得——点击EditText的时候,输入法把整体布局顶出了屏幕?这里提供一个我个人的解决办法
- Android字数限制的EditText实现方案研究