【问题标题】:How to make a disabled spinner not to look disabled?如何使禁用的微调器看起来不禁用?
【发布时间】:2019-10-17 15:23:40
【问题描述】:

我做了一个微调器,当上面只有一个项目时,我想禁用它,但我不喜欢禁用它时文本的灰色外观。我已经尝试使用.setClickable(false);,但它不起作用。我看过很多帖子,相反,禁用的帖子看起来与默认的不同,但这对我没有帮助。
我现在的代码是:

if (list.size() > 1) {
    list.setlistNumber("Select a list");
    list.add(0, list);
    mListSpinner.setClickable(true);
    return list;
} else {
    mAccountSpinner.setEnabled(false);
    // mListSpinner.set;
    t = 1;

但我不知道如何获得 Enable(false),看起来像 Enable(true) 而不是可选择的。

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    希望对你有帮助。

    在xml中

               <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:gravity="center">
    
                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp" />
    
                <LinearLayout
                    android:id="@+id/layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#00000000"
                    android:clickable="false"
                    android:orientation="vertical" />
            </RelativeLayout>
    

    在java中:

    这是我测试过的代码。

     List<String> list = new ArrayList<>();
        list.add("Please select a item");
        list.add("Item1");
        list.add("Item2");
        list.add("Item3");
    
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, list);
    
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setAdapter(adapter);
    
        if (list.size() > 1) {
            layout.setClickable(false);
        } else {
            layout.setClickable(true);
        }
    

    【讨论】:

    • 试过这个,不能作为微调器的默认工作方式,即使它是假的,也可以使可点击为真,(或者至少在我尝试时它对我不起作用)
    • 我已经更新了我测试过的java代码。见上文
    • 我不知道是不是我的电脑,但我无法工作,但如果有人告诉我为他们工作,我会接受这个
    【解决方案2】:

    只需删除侦听器,以便除了保持默认行为之外不做任何事情。

    spinner.setOnClickListener(null);
    

    如果要再次启用微调器,只需再次添加监听器

    spinner.setOnClickListener(myListener);
    

    【讨论】:

    • 我试过这个并得到了这个异常:java.lang.RuntimeException:不要调用 setOnClickListener。您可能想要 setOnItemClickListener 代替。我也尝试了 setOnItemClickListener 但得到了另一个例外: setOnItemClickListener 不能与微调器一起使用。有什么想法吗?
    【解决方案3】:

    使用没有禁用的可绘制对象的自定义样式背景覆盖。

       <Spinner
          ...
          android:background="@drawable/spinner_background"
          ...
       />
    

    res/drawable/spinner_background.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- changed from apptheme_spinner_disabled_holo_light -->
    <item android:state_enabled="false"
          android:drawable="@drawable/apptheme_spinner_default_holo_light" />
    <item android:state_pressed="true"
          android:drawable="@drawable/apptheme_spinner_pressed_holo_light" />
    <item android:state_pressed="false" android:state_focused="true"
          android:drawable="@drawable/apptheme_spinner_focused_holo_light" />
    <item android:drawable="@drawable/apptheme_spinner_default_holo_light" />
    

    drawable 将取决于您的应用的风格。例如,您可以生成新的全息样式可绘制对象here

    至于文字颜色,这个应该很相似,加个类似的xml描述符:-

       <Spinner
          ...
          android:textColor="@color/spinner_text"
          ...
       />
    

    res/values/spinner_text.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:state_focused="true" android:color="@color/black"/>
       <item android:state_pressed="true" android:state_enabled="false" android:color="@color/black" />
       <item android:state_enabled="false" android:color="@color/black" />
       <item android:color="@color/black"/>
    </selector>
    

    不确定是否需要,您可以直接设置颜色:

           <Spinner
          ...
          android:textColor="#000000"
          ...
       />
    

    【讨论】:

    • 你能详细说明一下吗?否则我认为它应该是一个评论,我也不是很擅长 android 但问题不是背景而是文本,这会解决这个问题吗?
    • 是的,我的意思是在我有时间的时候添加细节,而这从未发生过。这应该会阻止文本和背景发生变化
    【解决方案4】:

    你可以试试这个:

    spinner.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多