自己在做app数字输入的时候,看到挖财记账的记一笔中的输入效果,感觉很是舒服,于是决定自己撸一个
先看看挖财的效果:
有两种方式可以实现:
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>
以及最终效果: