Java 如何使警报对话框在拖动搜索栏时仅显示一次

Java 如何使警报对话框在拖动搜索栏时仅显示一次,java,android,android-alertdialog,Java,Android,Android Alertdialog,好的,当我拉动搜索栏时,警报对话框会崩溃,并在avd上出现多次。 如何修复它,以便在ETCash中没有用户输入时,警报对话框在屏幕上显示一次。 这是不断提示用户输入无效信息,直到用户在我创建的edittext中输入内容。 任何建议!!! 我什么都试过了,但是没有用 import java.text.DecimalFormat; import junit.framework.Assert; import android.os.Bundle; import android.app.Activity

好的,当我拉动搜索栏时,警报对话框会崩溃,并在avd上出现多次。 如何修复它,以便在ETCash中没有用户输入时,警报对话框在屏幕上显示一次。 这是不断提示用户输入无效信息,直到用户在我创建的edittext中输入内容。 任何建议!!! 我什么都试过了,但是没有用

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);

        }
    });
}
导入java.text.DecimalFormat;
导入junit.framework.Assert;
导入android.os.Bundle;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.SeekBar;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
私人SeekBar sbCash;
私人按钮btnten、btntwenty、btnthirty;
私有文本视图tvShwProg,TVSFinal;
私人现金;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCash=(EditText)findViewById(R.id.etCash);
sbCash=(SeekBar)findviewbyd(R.id.sbCash);
tvShwProg=(TextView)findViewById(R.id.tvShwProg);
tvfinal=(TextView)findViewById(R.id.tvfinal);
btnten=(按钮)findViewById(R.id.btnten);
btntwenty=(按钮)findViewById(R.id.btntwenty);
btnthirty=(按钮)findViewById(R.id.btnthirty);
addTextChangedListener(新的TextWatcher(){
public void PostTextChanged(可编辑arg0)
{
}
更改前的公共void(字符序列arg0、int arg1、int arg2、int arg3)
{
}
public void onTextChanged(字符序列arg0、int arg1、int arg2、int arg3){
试一试{
guiChanged();
}
捕获(例外e)
{
e、 printStackTrace();
AlertDialog alert=errormessage();

alert.show();//为什么不禁用搜索栏直到它可以使用?为什么不将alert对话框设置为singleton,并且仅在它尚未显示时显示它?我需要alert对话框作为一项要求,并且我尝试在它已经显示时显示它,但它无法工作,因为它会自动取消它。“我试图在它已经显示时显示它,但它无法工作,因为它会自动取消。”-我告诉过你不要在显示时显示它