突出显示所选的微调器项

时间:2013-06-10 11:32:18

标签: android button radio-button spinner

我在wildnove's answer之后使用Spinner实施了自定义Button。一切正常,但我无法显示所选按钮的突出显示的单选按钮。

以下是代码。

((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // How to highlight Radio button of a selected Item???

            final String[] items = view.getResources().getStringArray(R.array.planets__entries);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
            new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ((Button) findViewById(R.id.btnSpinnerPlanets)).setText(items[which]);
                    dialog.dismiss();
                }
            }).create().show();
        }
    });     

有人可以帮我突出显示所选项目的单选按钮......

2 个答案:

答案 0 :(得分:0)

此代码的问题在于,每次单击Spinner时都会创建Button。请尝试以下代码:

    @Override
        protected Dialog onCreateDialog(int id) {
            Dialog dialog;
            AlertDialog.Builder builder;
            switch(id) {
            case 1:
                Button b=((Button) findViewById(R.id.btnSpinnerPlanets));
                builder = new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(get_the_adapter(b), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        b.setText(b.getResources().getStringArray(R.array.planets__entries)[which]);
                        dismissDialog(1);
                    }
                })
                dialog = builder.create();
                break;
            default:
                dialog = null;
            }
            return dialog;

        }
    }

    public ArrayAdapter<String> get_the_Adapter(Button view){
    String[] items = view.getResources().getStringArray(R.array.planets__entries);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
                return adapter;
    }

对于Button的{​​{1}}:

onClick()

答案 1 :(得分:0)

不幸的是,这种行为本身并不是在Spinner组件中实现的,但是,你总是可以创建自己的BaseAdapter,以显示你自己或者在下拉列表中你需要的天气,如下所示:

private class ExampleAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
                    //Here is where you actually get the chance to return whatever you want in the spinner component (the single bar with the arrow)
        return yourCommonView;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
              //Here is where you get the chance to return whatever you want in the dropdown menu so here you should validate what's the currently selected element and return an image accordingly...
        return yourSelectedView;
    }

}

这里重要的方法是,getDropDownView是一个让你有机会返回一个带有已检查CheckBox的元素或者你想要使用的任何标记的方法,当然你必须创建自己的布局并验证元素是否正确目前创建的需要标记或不标记...

问候!