Android EditText小实验

第一次在CSDN上面写博客,不知道写些什么东西,就将最近两天做的一个小实验记录下来。

这个小实验源自最近做的一个购物APP的一部分,当我们输入或者点击“+”或“-”选择好购买商品的数量,系统会自动计算出所需的费用,如图所示:

Android EditText小实验_第1张图片

下面是demo的代码:

package com.lishangjie.edittexttet;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private EditText input;
	private TextView result, price;
	private ImageView add, delete;
	private double pri;
	private DecimalFormat df;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		input = (EditText) findViewById(R.id.input);
		result = (TextView) findViewById(R.id.result);
		add = (ImageView) findViewById(R.id.add);
		delete = (ImageView) findViewById(R.id.delete);
		price = (TextView) findViewById(R.id.price);
		pri = Double.valueOf(price.getText().toString());
		df = new DecimalFormat("#.00");

		add.setOnClickListener(this);
		delete.setOnClickListener(this);

		input.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if (s.toString() == null || s.toString().equals("")) {
					s = "0";
				}
				int a = Integer.parseInt(s.toString());
				double c = a * pri;
				result.setText(String.valueOf(df.format(c)));
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.add:
			String addnumString = input.getText().toString();
			System.out.println("addnumString===" + addnumString);
			if (addnumString == null || addnumString.equals("")) {
				addnumString = "0";
			}
			int addnumInt = Integer.parseInt(addnumString) + 1;
			input.setText(String.valueOf(addnumInt));
			result.setText(String.valueOf(df.format(addnumInt * pri)));
			break;

		case R.id.delete:
			String delnumString = input.getText().toString();
			System.out.println("delnumString==" + delnumString);
			if (delnumString == null || delnumString.equals("")) {
				delnumString = "0";
			}
			int delnumInt = Integer.parseInt(delnumString) - 1;
			if (delnumInt >= 0) {
				input.setText(String.valueOf(delnumInt));
				result.setText(String.valueOf(df.format(delnumInt * pri)));
			}
			break;

		default:
			break;
		}
	}

}

布局文件的代码:



    

        

            

            

            
        

        

            
            

            

            

            
            

            
        
    


源码:

android edittext小实验

你可能感兴趣的:(Android开发)