onClick on custom text view not working on android version less than 5

时间:2017-08-04 12:46:58

标签: android

To use my font, I used Costume TextView with the following code :

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
        setFont();
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont();
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFont();
    }

    private void setFont() {
        Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/BNazanin.ttf");
        setTypeface(font, Typeface.NORMAL);
    }


}

and on layout :

<... .CustomTextView
       ...
        android:onClick="loadongallery"
         />

and on activity :

public void loadongallery(View v){
...
}

But by clicking on it on Android version less than 5, nothing happens. I need both fonts and clicks, please help me.

1 个答案:

答案 0 :(得分:1)

Add one more attribute to the textview in xml:

android:clickable="true"