在android系统的EditText的计算百分比值 [英] Calculating percentages value of edittext in android

查看:773
本文介绍了在android系统的EditText的计算百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了code以添加多个值的EditText 字段和一个字段显示总。现在,我需要包含两个的EditText 字段的百分比计算,总价值一起。

I have got the code to add the values of multiple edittext fields and display the total in one field. Now I need to include the percentage calculation of two more edittext fields along with the total value.

editTexts = new EditText[] {monthly_rent_et, water_et,eb_et,sewage_et,maint_et,others_et,sec_deposit_et};
    for (int i=0 ; i<editTexts.length ; i++) {
        editTexts[i].setOnFocusChangeListener(mFocusChangeListener);
    }

private View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        int total = 0;
        for (int i=0 ; i<editTexts.length-1 ; i++) {

            try {
                total += Integer.valueOf(editTexts[i].getText().toString());
            }
            catch(Exception e) {}
        }
        total_et.setText(total + "");

    }
};

现在我需要计算服务税的百分比(月租金值*服务税值)/ 100,这总。还罚百分比同样喜欢服务税。服务税和处罚两个比较的EditText 字段,每月的租金已经包括在上述code。我怎样才能得到呢?

Now I need to calculate the service tax percentage (monthly rent value*service tax value)/100 and and this to total. Also penalty percentage as same like service tax. Service tax and penalty are two more edittext fields and the monthly rent is already included in the above code. How can I get this?

推荐答案

假设 service_tax_et penalty_et 是新的 editTexts ,不要将其手动添加到阵列,而不是设置 focusChangeListener 他们,然后再尝试这样的:

Suppose service_tax_et and penalty_et are new editTexts, don't add them to the array instead set focusChangeListener on them manually and then try like this:

private View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    float total = 0;
    for (int i=0 ; i<editTexts.length-1 ; i++) {
        try {
            total += Integer.valueOf(editTexts[i].getText().toString());
        }
        catch(Exception e) {}
    }

    try {
        float service_tax_perc = (Integer.valueOf(monthly_rent_et.getText().toString()) + Integer.valueOf(service_tax_et.getText().toString())) / 100;
        total += service_tax_perc;
    } catch (Exception e) {}

    try {
        float penalty_perc = (Integer.valueOf(monthly_rent_et.getText().toString()) + Integer.valueOf(penalty_et.getText().toString())) / 100;
        total += penalty_perc;
    } catch (Exception e) {}

    total_et.setText(total + "");
}};

这篇关于在android系统的EditText的计算百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆