Android 单选按钮在单选按钮后自动取消选中。setChecked(false)
在我的活动Android 单选按钮在单选按钮后自动取消选中。setChecked(false),android,Android,在我的活动onCreate中,我将onCheckedChangeListener设置为一个无线电组。 在某些时候,我需要以编程方式取消选中单选组中任何选中的单选按钮,以便取消设置侦听器,取消选中按钮,并将侦听器设置回原来的位置,以防止在以编程方式取消选中按钮时它触发 当第一次检查单选按钮时,它通过意图拍摄照片。在onActivityResult的部分,如果用户没有拍照,我将取消选中该按钮。执行此操作后,无论何时选中单选按钮都会自动取消选中。我如何解决这个问题 这是我的代码: @Overr
onCreate
中,我将onCheckedChangeListener
设置为一个无线电组。
在某些时候,我需要以编程方式取消选中单选组中任何选中的单选按钮,以便取消设置侦听器,取消选中按钮,并将侦听器设置回原来的位置,以防止在以编程方式取消选中按钮时它触发
当第一次检查单选按钮时,它通过意图拍摄照片。在onActivityResult的部分,如果用户没有拍照,我将取消选中该按钮。执行此操作后,无论何时选中单选按钮都会自动取消选中。我如何解决这个问题
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist_vestuarios);
instanciar_objetos();
instanciar_listeners();
}
private void instanciar_objetos(){
ctx = getApplicationContext();
rg_1 = findViewById(R.id.radio_1_1);
}
private void instanciar_listeners(){
listener_radio_groups = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
last_rg = radioGroup;
seleccionar_radio((RadioButton) findViewById(last_rg.getCheckedRadioButtonId()));
}
};
rg_1.setOnCheckedChangeListener(listener_radio_groups);
}
private void seleccionar_radio(RadioButton rb){
String tag = (String) rb.getTag();
String[] s = tag.split("_");
seccion = s[0];
item = s[1];
radio = s[2];
if(radio.equalsIgnoreCase("1")){
//MARCÓ SI, MANDARLE CON LA FOTO
fotografia();
}
else{
//MARCO OTRA OPCION, PEDIR TEXTO
}
}
public void fotografia(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
return;
}
// Continue only if the File was successfully created
if (photoFile != null) {
try{
Uri photoURI = FileProvider.getUriForFile(ctx,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 1);
}
catch (Exception e){
e.printStackTrace();
}
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
String imageFileName = timeStamp;
String root = Environment.getExternalStorageDirectory().toString() + "/hrtech/hites/originales";
File storageDir = new File(root, "/"+seccion+"/"+item);
storageDir.mkdirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
// Show the thumbnail on ImageView
Uri imageUri = Uri.parse(mCurrentPhotoPath);
final File file = new File(imageUri.getPath());
// ScanFile so it will be appeared on Gallery
MediaScannerConnection.scanFile(ctx,
new String[]{imageUri.getPath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
else{
Toast.makeText(ctx,"Obligatoriamente debe tomar una foto para marcar \"SI\" en este ítem.",Toast.LENGTH_LONG).show();
last_rg.setOnCheckedChangeListener(null);
RadioButton aux = (RadioButton) findViewById(last_rg.getCheckedRadioButtonId());
aux.toggle();
last_rg.setOnCheckedChangeListener(listener_radio_groups);
}
}
你的意图是什么?radiobutton.setChecked(假);将取消选中收音机按钮。我尝试使用setChecked(false)并按预期取消选中收音机,但在执行此操作后,每当我再次检查收音机时,它都会立即取消选中。显示你的Selecionar\u收音机
我编辑了我的帖子并添加了完整的代码。Selecciator_收音机是不相关的,它只是一个基于所选收音机标签的功能