好的,所以当我拉动搜索栏时,警报对话框会惊慌失措并在avd上多次出现。 如何修复它,以便在ETCash中没有用户输入时,屏幕上显示警告对话框ONCE。 这将继续提示用户无效输入,直到用户在我创建的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);
}
});
}