Android数字动态显示,仿支付宝app效果简单的实现。字数从0-x!!!
来源:互联网 发布:lol域名不能备案 编辑:程序博客网 时间:2024/05/21 09:25
今天看到了,支付宝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;
-
-
-
-
-
-
-
- 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() {
-
- return super.getText();
- }
-
- public void setText(CharSequence text, BufferType type, Handler myHandler) {
- handler = myHandler;
- oldnum = 0.00;
- newnum = Double.valueOf(text.toString());
-
-
- 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;
-
-
-
-
-
-
-
- public class NumberTextViewActivity extends Activity {
- private AutoNumber numText;
- private EditText edit;
- private Button btn01;
- Handler handler;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- 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);
-
- }
- });
- }
- }
到此就完工了,有兴趣的去试试吧。转载注明出处:http://blog.csdn.net/just_sanpark/article/details/17920369
0 0