3

我已经Spinner使用Button以下wildnove 的答案实现了自定义。一切正常,但我无法为所选按钮显示突出显示的单选按钮。

下面是代码。

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

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

4

2 回答 2

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's onClick()

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

        }
    });    
于 2013-06-17T14:41:58.933 回答
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 的元素或您要使用的任何标记,当然您必须创建自己的布局并验证当前创建的元素是否需要标记与否...

问候!

于 2013-06-18T23:06:47.033 回答