0

好的,所以当我拉搜索栏时,警报对话框吓坏了,并在 avd 上多次出现。我该如何修复它,以便当 ETCash 中没有用户输入时,警报对话框会显示在屏幕上一次。这会一直提示用户输入无效,直到用户在我创建的编辑文本中输入内容。有什么建议!!!已经尝试了一切,但无济于事!

import java.text.DecimalFormat;

import junit.framework.Assert;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; 
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {



private SeekBar sbCash;
private Button btnten,btntwenty,btnthirty;
private TextView tvShwProg,tvfinal;
private EditText etCash;


@Override
public void onCreate(Bundle savedInstanceState)
{


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    etCash = (EditText)findViewById(R.id.etCash);
    sbCash = (SeekBar)findViewById(R.id.sbCash);
    tvShwProg = (TextView)findViewById(R.id.tvShwProg);
    tvfinal = (TextView)findViewById(R.id.tvFinal);
    btnten = (Button)findViewById(R.id.btnten);
    btntwenty = (Button)findViewById(R.id.btntwenty);
    btnthirty = (Button)findViewById(R.id.btnthirty);




    etCash.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable arg0) 
        {


        }
        public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) 
        {

        }
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

            try{
                  guiChanged();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    AlertDialog alert =errormessage();
                    alert.show(); //<<< show AlertDialog here
                }
        }


    });


    SeekBar.OnSeekBarChangeListener sbListener = new SeekBar.OnSeekBarChangeListener() 
    {
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

        public void onStartTrackingTouch(SeekBar seekBar) {}
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
        {
            try{
            guiChanged();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }


    };
    sbCash.setOnSeekBarChangeListener(sbListener);

    dosomethig();
    dosomethig2();
    dosomething3();




}

public AlertDialog errormessage()
{
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Enter a Number!!!!")
           .setNegativeButton("Fix", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.show();
}


public void guiChanged()
{
        try{
            DecimalFormat fmt = new DecimalFormat ("0.00");

            int CashMoney = sbCash.getProgress();

            tvShwProg.setText("Current Percentage " +CashMoney+ "%");

            double amount = Double.parseDouble(etCash.getText().toString());

            double moneydue = (amount * (CashMoney/100.0));

            double totalamount = (moneydue + amount);

            tvfinal.setText("Tip " + fmt.format(moneydue)+ " Total "+ fmt.format(totalamount));
        }
        catch(Exception e)
        {
            e.printStackTrace();

            AlertDialog alert = errormessage();
            alert.show();
            if(alert.isShowing())
            {
                alert.cancel();
            }
        }

}


public void dosomethig()
{

    btnten.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            sbCash.setProgress(10);


        }
    });
}
public void dosomethig2()
{

    btntwenty.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            sbCash.setProgress(15);
        }


    });
}
public void dosomething3()
{
    btnthirty.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            sbCash.setProgress(20);

        }
    });
}
4

0 回答 0