简体   繁体   中英

setEnabled doesnt work in android

I have this code:

public TextView main_text;//begining of the class

main_text = (TextView) findViewById(R.id.TextMain); //inside OnCreate

main_text.setEnabled(false); //inside button handler

And now the part of Xml

    <TextView
        android:id="@+id/TextMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="left"
        android:textColor="#FFFFFF"
        android:text="@string/home_load" >

    </TextView>

Why doesnt SetEnable work? It seems so obvious that it should.

What do you expect setEnabled(false) to do to a TextView?

If you want to hide the TextView, you should rather call setVisibility(View.INVISIBLE)

If you want to disable clicks, you should rather call setOnClickListener(null)

If you want the text to display in a disabled state, then you need to define the states for the view in a separate XML file.

For example textView.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/enabled" />
    <item android:state_enabled="false" android:color="@color/disabled" />
</selector>

And then in your TextView definition use

android:textColor="@drawable/textView"

Views can be composed by multiple touchable elements. You have to disable them all, like this:

for(View lol : text_view.getTouchables() ) {
    lol.setEnabled(false);
}

If it is a simple one since it also returns itself:

Find and return all touchable views that are descendants of this view, possibly including this view if it is touchable itself.

View#getTouchables()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM