读出是否在其他控制器中选择了RadioButton

时间:2018-08-28 15:47:26

标签: java javafx fxml

我目前正在JavaFX Project上工作,如果选择了RadioButton,则需要从另一个类访问FXML对象以显示ComboBox。

例如,我有4个名为的单选按钮

//First Controller
@FXML
private RadioButton radioButtonS1, radioButtonS2, radioButtonS3, radioButtonS4;

我必须在另一个Controller中读出它们,以使其可见,我的ComboBox称为:

//Second Controller
@FXML
private ComboBox comboS1A, comboS1E1, comboS1E2;

@FXML
private ComboBox comboS2A, comboS2E1, comboS2E2;

@FXML
private ComboBox comboS3A, comboS3E1, comboS3E2;

@FXML
private ComboBox comboS4A, comboS4E1, comboS4E2;

那么我如何在SecondController中看到在FirstController中选择了哪个RadioButton并使CombBox可见?

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以创建静态int变量,并且该变量包含选定的RadioButton号

if(ContextCompat.checkSelfPermission(A1.this,
            Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        if (ActivityCompat.shouldShowRequestPermissionRationale(A1.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)){
            ActivityCompat.requestPermissions(A1.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST);
        } else {
            ActivityCompat.requestPermissions(A1.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST);
        }
    } else {
        doStuff();
    }
}

public void doStuff(){
    arrayList = new ArrayList<>();
    getVideo();
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String  myUrl = adapter.getItem(position);
            Uri uri = Uri.parse(myUrl);
            videoView1.setVideoURI(uri);
            videoView1.start();

        }
    });
}
public void getVideo(){
    ContentResolver contentResolver = getContentResolver();
    Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    Cursor videoCursor = contentResolver.query(videoUri,null,null,null,null);

    if(videoCursor != null && videoCursor.moveToFirst()) {
        int songTitle = videoCursor.getColumnIndex(MediaStore.Video.Media.TITLE);
        int songAlbum = videoCursor.getColumnIndex(MediaStore.Video.Media.ALBUM);

        do{
            String currentTitle = videoCursor.getString(songTitle);
            String currentAlbum = videoCursor.getString(songAlbum);
            arrayList.add(currentTitle + "\n" + currentAlbum);
        }while(videoCursor.moveToNext());
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSION_REQUEST: {
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if(ContextCompat.checkSelfPermission(A1.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(A1.this, "Permission Granted" , Toast.LENGTH_SHORT).show();

                    doStuff();
                } else {
                    Toast.makeText(A1.this, "No Permission granted" , Toast.LENGTH_SHORT).show();

                    finish();
                }
                return;
            }
        }
    }
}

并将此行放入第一个控制器的初始化方法中

public static int selectedCombo = -1;

在第二个控制器中,您需要进行切换:

radioButtonS1.setOnAction(e->{
       selectedCombo = 0;
});

radioButtonS2.setOnAction(e->{
   selectedCombo = 1;
});
...