Android alertdialog的按钮点击后不消失


 private void ShowAppleDialog() {
  AlertDialog.Builder db = new AlertDialog.Builder(this);
  LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View view = inflater.inflate(R.layout.apple_dialog, null);
  db.setView(view);
  db.setIcon(R.drawable.bg_settings_min);
  db.setTitle(getString(R.string.device_settings));
  
  dialogDeviceName = (EditText)view.findViewById(R.id.dialog_device_name);
  dialogPassword = (EditText)view.findViewById(R.id.dialog_device_pas);
  showPassword = (CheckBox)view.findViewById(R.id.showPassword);
  StatusOn = (RadioButton)view.findViewById(R.id.status_on);
  StatusOff = (RadioButton)view.findViewById(R.id.status_off);
  
  StatusOn.setOnClickListener(this);
     StatusOff.setOnClickListener(this);
  
  SharedPreferences pres = getSharedPreferences(APPLE_DEVICE,Context.MODE_WORLD_READABLE);
  if(pres != null){
   mDeviceName = pres.getString("DEVICE_NAME", Build.MODEL);
   mPassword = pres.getString("PASSWORD", "");
   dialogDeviceName.setText(mDeviceName);
   dialogPassword.setText(mPassword);
   mDeviceStatus = pres.getBoolean("DEVICE_STATUS", true);
   if(mDeviceStatus){
    StatusOn.setChecked(true);
   }else{
    StatusOff.setChecked(true);
   }
  }
  showPassword.setChecked(false);
  showPassword.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        if (showPassword.isChecked())
          dialogPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
               else
                dialogPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
           
   }
  });
  
  db.setPositiveButton(getString(R.string.app_save), new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog,int which) {
    String mName = dialogDeviceName.getText().toString();
    if(mName.length() == 0){
     Toast.makeText(mContext, getString(R.string.device_name_null),Toast.LENGTH_LONG).show();
     //用于不关闭对话框
     try{
      Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
      field.setAccessible(true);
      field.set(dialog, false);
      } catch (Exception e) {
      e.printStackTrace();
     }
    }else{
     SharedPreferences pres = getSharedPreferences(APPLE_DEVICE,Context.MODE_WORLD_READABLE);    
     if(pres != null){
      final Editor ed = pres.edit();
      ed.putString("DEVICE_NAME",dialogDeviceName.getText().toString());
      ed.putString("PASSWORD",dialogPassword.getText().toString());
      ed.putBoolean("DEVICE_STATUS", isDeviceStatus);     
      ed.commit();
      Log.d(TAG, "Success save device info ...");
      mDeviceName = pres.getString("DEVICE_NAME", Build.MODEL);
      mPassword = pres.getString("PASSWORD", "");
      mDeviceStatus = pres.getBoolean("DEVICE_STATUS", true);
         deviceName.setText(mDeviceName);
         if (mDeviceStatus){
       deviceStatus.setText(getString(R.string.device_on));
      }else{
       deviceStatus.setText(getString(R.string.device_off));
      }
//       Intent intent1 = new Intent();
//          intent1.setAction("dapple.intent.action.SETTING_FINISH");
//          intent1.putExtra("player_name", mDeviceName);
//          intent1.putExtra("player_password", mPassword);
//          intent1.putExtra("service", "on");
//          sendBroadcast(intent1);
     }else{
      Log.d(TAG, "device info Failure...");
     }
     //用于关闭对话框

     try {
      Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
      field.setAccessible(true);
      field.set(dialog, true);
      } catch (Exception e) {
      e.printStackTrace();
      }
    }       
   }
  });
  db.setNegativeButton(getString(R.string.app_cancel), new DialogInterface.OnClickListener() {
   
   @Override
   public void onClick(DialogInterface dialog, int which) {
    //用于关闭对话框

    try {
     Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
     field.setAccessible(true);
     field.set(dialog, true);
     } catch (Exception e) {
     e.printStackTrace();
     }
   }
  });
  AlertDialog d = db.create();
  d.setInverseBackgroundForced(true);
  d.show();
 }

猜你喜欢

转载自haiyang08101.iteye.com/blog/1752456