具有提示和默认值的Edittext是否可能?

时间:2014-09-15 21:29:03

标签: java android android-edittext default hint

我正在创建一个小费计算器应用程序,我想知道如何允许我的edittext(用户输入账单金额)仍然显示提示(提示说"输入账单金额")但内部也有默认值为零。我想这样做的原因是因为如果他们按下计算提示按钮,并且在编辑文本中没有输入文本,应用程序强制关闭,我认为默认值为零可以解决我的问题,因为它只会说小费$ 0.00。或者,如果有人可以告诉我如何弹出一个说他们需要输入更好的账单金额!

Current Main Java:

    package com.rockdrummer.calculating.calculating;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import java.text.NumberFormat;
    import java.util.Locale;


    public class MainScreen extends Activity {
        private static final NumberFormat currencyFormat= NumberFormat.getCurrencyInstance();

        Button ten, fifteen, twenty;
        TextView tip;
        EditText bill_amount;
        double a = 0;
        double b;
        double af = 0;
        double bf;
        double ac = 0;
        double bc;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);
    initControls();
    Locale.setDefault(Locale.US);




}



protected void initControls() {
    ten = (Button) findViewById(R.id.bTen);
    fifteen = (Button) findViewById(R.id.bFifteen);
    twenty = (Button) findViewById(R.id.bTwenty);
    tip = (TextView) findViewById(R.id.tip);
    bill_amount = (EditText) findViewById(R.id.bill);






    ten.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View ten) {


            try {
                calculate();
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
    });
    fifteen.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View fifteen) {
            try {
                calculate_f();
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
    });
    twenty.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View twenty) {
            try {
                calculate_t();
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
    });
}

private void calculate() {


    a = 0 + Double.parseDouble(bill_amount.getText().toString());

    b = 0 + (a * 0.1);
    tip.setText("You Should Tip " + currencyFormat.format(b));
}

private void calculate_f() {
    af = 0 + Double.parseDouble(bill_amount.getText().toString());
    bf = 0 + (af * 0.15);
    tip.setText("You Should Tip " + currencyFormat.format(bf));
}

private void calculate_t() {
    ac = 0 + Double.parseDouble(bill_amount.getText().toString());
    bc = 0 + (ac * 0.20);
    tip.setText("You Should Tip " + currencyFormat.format(bc));
}

}

1 个答案:

答案 0 :(得分:0)

只需检查onClick / onCalculate方法中的文本是否为空。如果为空,请使用0,即:

public void onCalculate() {
    float input = 0.0;
    if (editText.getText().toString().equals("")) {
         //Text is empty do nothing
    } else {
         //parse your value
         input = Float.parseFloat(editText.getText().toString());
    }
}