java Android Spinner - 如何删除单选按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1891809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:24:29  来源:igfitidea点击:

Android Spinner - How to remove radio buttons?

javaandroidandroid-widgetspinner

提问by iamkoa

In Android 1.6, upon tapping a spinner (drop-down menu), radio buttons appear next to the spinner options. How do I remove those radio buttons so that just the option text remains?

在 Android 1.6 中,点击微调器(下拉菜单)后,微调器选项旁边会出现单选按钮。如何删除这些单选按钮,以便只保留选项文本?

回答by kimkunjj

Just to remove the radio buttons, you don't need your own adapter class.

只是为了删除单选按钮,您不需要自己的适配器类。

Create a dropdown_item.xml in layout

在布局中创建一个 dropdown_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

then make the following call in the code.

然后在代码中进行以下调用。

arrayAdapter.setDropDownViewResource(R.layout.dropdown_item);

The default spinner dropdown item is a CheckedTextView which has the radio button. Here you replace it with a TextView.

默认的微调下拉项是一个 CheckedTextView,它有一个单选按钮。在这里,您将其替换为 TextView。

回答by TouchBoarder

You can use the android layout

您可以使用android布局

android.R.layout.simple_spinner_item 

instead of

代替

android.R.layout.simple_spinner_dropdown_item

but I recommend @kimkunjjanswer, it will give you the control of the layout.

但我推荐@kimkunjj回答,它会给你布局的控制权。

回答by Ramps

If you want to get rid of radio buttons on the spinner list you have to provide your own layout for row.
Take a look at the example below:

如果你想去掉微调列表上的单选按钮,你必须为行提供你自己的布局。
看看下面的例子:


package com.ramps;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class MySpinner extends Activity {
    //data that will be used as a spinner options
    private static String data[] = {"one", "two", "three"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //main.xml file contains spinner
        setContentView(R.layout.main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        //create your own adapter
        MySpinnerAdapter adapter = new MySpinnerAdapter(this,R.layout.custom_spinner_row,R.id.text, data );
        //set your custom adapter 
        spinner.setAdapter( adapter );
    }


    private class MySpinnerAdapter extends ArrayAdapter{

        public MySpinnerAdapter(Context context, int resource,
                int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);          
        }   

    }
}


The custom layout for spinner row is just a simple LinearLayout with one TextView element which id is "text" (android:id="@+id/text")

This is just simple example. If you need more fancy layout than just TextView you would probably have to override getView() method of MySpinnerAdapter.


Spinner 行的自定义布局只是一个简单的 LinearLayout,带有一个 TextView 元素,id 为“text” (android:id="@+id/text")

这只是一个简单的例子。如果您需要比 TextView 更精美的布局,您可能必须覆盖 MySpinnerAdapter 的 getView() 方法。

回答by Neoh

If you have included android support library version 18 or above into your project then you can replace the resource id simple_spinner_dropdown_itemwith support_simple_spinner_dropdown_item. That will remove the radio button.

如果您已包括支持Android库版本18或以上到你的项目,那么你可以替换的资源IDsimple_spinner_dropdown_itemsupport_simple_spinner_dropdown_item。这将删除单选按钮。

回答by Ronny Kibet

use simple_dropdown_item_1line

利用 simple_dropdown_item_1line

回答by Mr. Clankey

"android.R.layout.simple_spinner_item" does the job,

“android.R.layout.simple_spinner_item”完成这项工作,

programmatically that's:

以编程方式是:

modeSpinner=new Spinner(layout.getContext());
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(layout.getContext(),     
    android.R.layout.simple_spinner_item, Arrays.asList(modes));
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

回答by Alex Semeniuk

The "cleanest" way of doing this (just remove the check mark and nottouch anythinbg else including text style, item size etc.) is to create custom adapter (like in the answers above):

这样做的“最干净”的方法(只需删除复选标记,不要触摸任何其他内容,包括文本样式、项目大小等)是创建自定义适配器(如上面的答案):

public class SimpleSpinnerArrayAdapter extends ArrayAdapter<String> {

    public SimpleSpinnerArrayAdapter(Context context, String[] data) {
        super(context, android.R.layout.simple_spinner_item, data);
        this.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    public SimpleSpinnerArrayAdapter(Context context, List<String> data) {
        super(context, android.R.layout.simple_spinner_item, data);
        this.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    /**
     * Returns default dropdown view with removed checkbox
     */
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        if (view != null && view instanceof CheckedTextView) {
            ((CheckedTextView) view).setCheckMarkDrawable(null);
        }
        return view;
    }
}

Please notice the getDropDownView()method which returns the view for dropdown list item. You can use any custom view here but if you would like to stick to the default view you should probably use the above code.

请注意getDropDownView()返回下拉列表项视图的方法。您可以在此处使用任何自定义视图,但如果您想坚持使用默认视图,您可能应该使用上面的代码。