当我选择否时,显示警报消息会显示两次,为什么?当我选择“否”时,如何仅显示一次警报?

时间:2019-07-08 07:13:20

标签: xamarin

公共异步无效clearswitchHandle_Toggled(对象发送者,     Xamarin.Forms.ToggledEventArgs e)         {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    textView1 = (TextView) findViewById(R.id.location_view);
    button1 = (Button) findViewById(R.id.camera);
    button2 = (Button) findViewById(R.id.upload);
    editText1 = (EditText) findViewById(R.id.remarks);
    imageView11 = (ImageView) findViewById(R.id.image1);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);


    locationText = (TextView) findViewById(R.id.location_view);
    if (ContextCompat.checkSelfPermission(getApplicationContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat
            .checkSelfPermission(getApplicationContext(),
                    android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission
                .ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
    }

    getLocation();
}



public void getLocation() {
    try {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, (LocationListener) this);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.item1:
            Toast.makeText(getApplicationContext(),"Account Clicked",Toast.LENGTH_SHORT).show();
            return true;
         case R.id.item2:
            Toast.makeText(getApplicationContext(),"Account Clicked",Toast.LENGTH_SHORT).show();
            return true;
         case R.id.item3:

             AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
             alerDialogbuilder.setTitle("Confirm Logout");
             alerDialogbuilder.setIcon(R.drawable.ic_error_black_24dp);
             alerDialogbuilder.setMessage("Are You Sure You Want to Logout ");
             alerDialogbuilder.setMessage("Logingout will need id password again");
             alerDialogbuilder.setCancelable(false);
             alerDialogbuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     Intent intents = new Intent(Main3Activity.this,MainActivity.class);
                     startActivity(intents);
                     Toast.makeText(getApplicationContext(),"Successfull Logout",Toast.LENGTH_SHORT).show();
                                         }
             });
             alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     Toast.makeText(getApplicationContext(),"Logout Canceled",Toast.LENGTH_SHORT).show();
                 }
             });
             AlertDialog alertDialog = alerDialogbuilder.create();
             alertDialog.show();


            return true;
           default:return super.onOptionsItemSelected(item);
    }
    }

private void camera(){
    Intent intents = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intents,CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==CAMERA_REQUEST&&resultCode== Activity.RESULT_OK){
        Bitmap photo= (Bitmap)data.getExtras().get("data");
        imageView11.setImageBitmap(photo);

    }
}

private void upload(){
    AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
    alerDialogbuilder.setTitle("Confirm Upload ?");
    alerDialogbuilder.setIcon(R.drawable.ic_error_black_24dp);
    alerDialogbuilder.setMessage("Are You Sure You Want to Upload Data");
    alerDialogbuilder.setCancelable(false);
    alerDialogbuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
          Toast.makeText(getApplicationContext(),"File Uploading...",Toast.LENGTH_SHORT).show();
        }
    });
    alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getApplicationContext(),"Recheck Data",Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alertDialog = alerDialogbuilder.create();
    alertDialog.show();
}

@Override
public void onClick(View view) {

    if(view==button1){
        camera();
    }

    else if (view == button2){
        upload();
    }

}


@Override
public void onLocationChanged(Location location) {

    double lati = location.getLatitude();
    double longi = location.getLongitude();

    locationText.setText("Latitude: " + lati + "\n Longitude: " + longi);

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

    Toast.makeText(this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
    }

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {
    AlertDialog.Builder alerDialogbuilder = new AlertDialog.Builder(Main3Activity.this);
    alerDialogbuilder.setTitle("Enable Gps to Continue");
    alerDialogbuilder.setIcon(R.drawable.ic_error_black_24dp);
    alerDialogbuilder.setMessage("If You Want To Enable Gps Go To Settings");
    alerDialogbuilder.setCancelable(false);
    alerDialogbuilder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent1);
            Toast.makeText(getApplicationContext(),"Enable Gps..",Toast.LENGTH_SHORT).show();
        }
    });
    alerDialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
            Toast.makeText(getApplicationContext(),"Uploading Failed,Enable Gps",Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alertDialog = alerDialogbuilder.create();
    alertDialog.show();
    }
  }

1 个答案:

答案 0 :(得分:1)

尝试锁定此异步方法,它可能会多次调用:

private bool lockAlert;
public async void Handle_Toggled(object sender, Xamarin.Forms.ToggledEventArgs e) 
{
        if (lockAlert) return;
        lockAlert=true;

        var ok = await DisplayAlert("WARNING!!!", "Are you sure you want to CLEAR DATA entered", "YES", "NO");
        if (ok)
        {
            clearButton.IsEnabled = true;
        }
        else 
        {
            clearSwitch.IsToggled = false;
        }

        lockAlert=false;
    }