今天看到了,支付宝app里面的数字从0到X感觉相当的不错,网上搜了下好像没有这方面的介绍,自己想了想做了个简单的实现。
先上程序截图:
代码:自定义控件布局:activity_number.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.android_ui.widget.AutoNumber android:id="@+id/num" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_margin="20dip" android:background="@android:color/transparent" android:gravity="center" android:singleLine="true" android:text="0" /> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="decimal" /> <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入"/> </LinearLayout>
自定义控件:AutoNumber
package com.example.android_ui.widget; import java.text.NumberFormat; import android.content.Context; import android.os.Handler; import android.util.AttributeSet; import android.widget.TextView; /** * 数字显示0-X * * @author Sanpark * */ public class AutoNumber extends TextView { private Handler handler; Runnable r = null; double oldnum; double newnum; double startnum = 0; public AutoNumber(Context context, Handler handler) { super(context); } public AutoNumber(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public AutoNumber(Context context, AttributeSet attrs) { super(context, attrs); } @Override public CharSequence getText() { // TODO Auto-generated method stub return super.getText(); } public void setText(CharSequence text, BufferType type, Handler myHandler) { handler = myHandler; oldnum = 0.00; newnum = Double.valueOf(text.toString()); // 转化国际化数字显示 // 若想在数字前面在上¥符号 ,将getIntegerInstance()方法改为getCurrencyInstance () 方法 final NumberFormat af = NumberFormat.getIntegerInstance(); // 设置精确到小数点后两位 af.setMinimumFractionDigits(2); r = new Runnable() { @Override public void run() { // 设置每次添加的度量 oldnum += newnum / 30; if (startnum < newnum) { if (oldnum > newnum) oldnum = newnum; setText(af.format(oldnum)); handler.postDelayed(r, 50); } else if (oldnum == newnum) { setText(af.format(oldnum)); } } }; handler.postDelayed(r, 50); } }最后测试Activity:NumberTextViewActivity
package com.example.android_ui; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.android_ui.widget.AutoNumber; /** * 测试界面 * * @author Sanpark * */ public class NumberTextViewActivity extends Activity { private AutoNumber numText; private EditText edit; private Button btn01; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_number); numText = (AutoNumber) findViewById(R.id.num); edit = (EditText) findViewById(R.id.editText); btn01 = (Button) findViewById(R.id.btn01); handler = new Handler(); btn01.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { numText.setText(edit.getText().toString(), TextView.BufferType.NORMAL, handler); } }); } }到此就完工了,有兴趣的去试试吧。