android - 突出显示选定的微调项

标签 android button radio-button spinner

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

下面是代码。

((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();
        }
    });     

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

最佳答案

不幸的是,这种行为并没有在 Spinner 组件中本地实现,但是,您始终可以创建自己的 BaseAdapter 以在 Spinner 本身或下拉列表中显示您需要的任何天气:

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 的元素,或者您想要使用的任何标记,当然您必须创建自己的布局并验证元素是否当前创建的是否需要标记...

问候!

关于android - 突出显示选定的微调项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17022918/

相关文章:

android - 我怎样才能在android中的线性布局中制作阴影效果?

android - 如何修复 - 使用 lowerCamelCase 命名非常量标识符

javascript - 如何向此 JavaScript 和 HTML 代码添加一个按钮,以便当我按下该按钮时,我的交通灯序列运行一次?

java - 用 JLayer 装饰滚动工具栏按钮绘制其边框

SwingUtilities.invokeLater()

java代码从zk框架中的数据库列条目获取单选按钮标签

javascript - 2 个阵列作为 1 个

android - “main cannot be resolved or is not a field”

android - 在图像 Android 上创建签名?

javascript - IE HTML 单选更改事件