下面列出了android.widget.TextView#setFocusable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* set SearchView OnClickListener
*
* @param v
* @param listener
*/
public static void setSearchViewOnClickListener(View v, OnClickListener listener) {
if (v instanceof ViewGroup) {
ViewGroup group = (ViewGroup)v;
int count = group.getChildCount();
for (int i = 0; i < count; i++) {
View child = group.getChildAt(i);
if (child instanceof LinearLayout || child instanceof RelativeLayout) {
setSearchViewOnClickListener(child, listener);
}
if (child instanceof TextView) {
TextView text = (TextView)child;
text.setFocusable(false);
}
child.setOnClickListener(listener);
}
}
}
static TextView newTitleView(Context context) {
TextView titleView = new TextView(context);
titleView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
titleView.setGravity(Gravity.CENTER);
titleView.setFocusable(true);
titleView.setSingleLine();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE_1_1) {
// 给标题设置跑马灯效果(仅在标题过长的时候才会显示)
titleView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
// 设置跑马灯的循环次数
titleView.setMarqueeRepeatLimit(-1);
// 设置跑马灯之后需要设置选中才能有效果
titleView.setSelected(true);
} else {
titleView.setEllipsize(TextUtils.TruncateAt.END);
}
return titleView;
}
private void addTextTab(final int position, String title) {
TextView tab = new TextView(this);
tab.setText(title);
tab.setTextSize(25);
tab.setFocusable(true);
tab.setGravity(Gravity.CENTER);
tab.setSingleLine();
tab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(position);
}
});
tab.setPadding(tabPadding, 0, tabPadding, 0);
tabsContainer.addView(tab, position);
}
@NonNull
private TextView getCourseTextView(int h, int w) {
TextView tv = new TextView(getContext());
LayoutParams params = new LayoutParams(w, h);
tv.setLayoutParams(params);
tv.setTextColor(mTextColor);
tv.setLineSpacing(-2, 1);
tv.setPadding(mTextLRPadding, mTextTBPadding, mTextLRPadding, mTextTBPadding);
tv.setTextColor(mTextColor);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextSize);
tv.setFocusable(true);
tv.setClickable(true);
//bold
TextPaint tp = tv.getPaint();
tp.setFakeBoldText(true);
return tv;
}
public TextPopup(Context context, ViewGroup parentView)
{
super(context, parentView);
text = new TextView(context);
text.setPadding((int)(PADDING_LEFT * dipScaleFactor),
(int)(PADDING_TOP * dipScaleFactor),
(int)(PADDING_RIGHT * dipScaleFactor),
(int)(PADDING_BOTTOM * dipScaleFactor));
text.setBackgroundResource(R.drawable.map_description_background);
text.setTextSize(DEF_TEXT_SIZE);
text.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
text.setMaxEms(MAX_EMS);
text.setTextColor(Color.WHITE);
container.addView(text);
text.setFocusable(true);
text.setClickable(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_description, container, false);
mTitleView = (TextView) rootView.findViewById(R.id.title);
mSummaryView = (TextView) rootView.findViewById(R.id.summary);
if (Utils.isTv(getActivity())) {
mSummaryView.setFocusable(true);
} else {
mTitleView.setTextIsSelectable(true);
mSummaryView.setTextIsSelectable(true);
}
mSummaryView.setSelected(true);
mSummaryView.setMovementMethod(LinkMovementMethod.getInstance());
mTitle = getArguments().getCharSequence("title");
mSummary = getArguments().getCharSequence("summary");
refresh();
return rootView;
}
/**
* init toolbar
*/
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// non-translatable logo
toolbar.setTitle("shadowsocks R");
toolbar.setTitleTextAppearance(toolbar.getContext(), R.style.Toolbar_Logo);
try {
Field field = Toolbar.class.getDeclaredField("mTitleTextView");
field.setAccessible(true);
TextView title = (TextView) field.get(toolbar);
title.setFocusable(true);
title.setGravity(0x10);
title.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Shadowsocks.this, ProfileManagerActivity.class));
}
});
TypedArray typedArray = obtainStyledAttributes(new int[]{R.attr.selectableItemBackgroundBorderless});
title.setBackgroundResource(typedArray.getResourceId(0, 0));
typedArray.recycle();
Typeface tf = Typefaces.get(this, "fonts/Iceland.ttf");
if (tf != null) {
title.setTypeface(tf);
}
title.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(getResources().getInteger(R.integer.GRID_ITEM_WIDTH), getResources().getInteger(R.integer.GRID_ITEM_HEIGHT)));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.default_background));
view.setTextColor(Color.WHITE);
view.setGravity(Gravity.CENTER);
return new ViewHolder(view);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(getResources().getColor(R.color.default_background));
view.setTextColor(Color.WHITE);
view.setGravity(Gravity.CENTER);
return new ViewHolder(view);
}
static TextView newLeftView(Context context) {
TextView leftView = new TextView(context);
leftView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
leftView.setGravity(Gravity.CENTER_VERTICAL);
leftView.setFocusable(true);
leftView.setClickable(true);
leftView.setSingleLine();
leftView.setEllipsize(TextUtils.TruncateAt.END);
return leftView;
}
static TextView newRightView(Context context) {
TextView rightView = new TextView(context);
rightView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
rightView.setGravity(Gravity.CENTER_VERTICAL);
rightView.setFocusable(true);
rightView.setClickable(true);
rightView.setSingleLine();
rightView.setEllipsize(TextUtils.TruncateAt.END);
return rightView;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(ITEM_WIDTH, ITEM_HEIGHT));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setTextColor(Color.WHITE);
view.setGravity(Gravity.CENTER);
view.setTextSize(32);
return new ViewHolder(view);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(getResources().getColor(R.color.default_background));
view.setTextColor(Color.WHITE);
view.setGravity(Gravity.CENTER);
return new ViewHolder(view);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(getResources().getColor(R.color.default_background));
view.setTextColor(Color.WHITE);
view.setGravity(Gravity.CENTER);
return new ViewHolder(view);
}
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView textView = new TextView(parent.getContext());
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
textView.setBackground(
parent.getContext().getResources().getDrawable(R.drawable.background_cone));
return new ViewHolder(textView);
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
int leftPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingLeft,
dipsToPix(DEFAULT_LABEL_PADDING_LEFT));
int topPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingTop,
dipsToPix(DEFAULT_LABEL_PADDING_TOP));
int rightPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingRight,
dipsToPix(DEFAULT_LABEL_PADDING_RIGHT));
int bottomPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingBottom,
dipsToPix(DEFAULT_LABEL_PADDING_BOTTOM));
mHint = a.getText(R.styleable.FloatLabelLayout_floatLabelHint);
mLabel = new TextView(context);
mLabel.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
mLabel.setVisibility(INVISIBLE);
mLabel.setText(mHint);
mLabel.setFocusable(true);
ViewCompat.setPivotX(mLabel, 0f);
ViewCompat.setPivotY(mLabel, 0f);
ApiCompatibilityUtils.setTextAppearance(mLabel,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
a.recycle();
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mInterpolator = AnimationUtils.loadInterpolator(context,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
? android.R.interpolator.fast_out_slow_in
: android.R.anim.decelerate_interpolator);
}
public MyViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.item);
textView.setFocusable(true);
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
int leftPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingLeft,
dipsToPix(DEFAULT_LABEL_PADDING_LEFT));
int topPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingTop,
dipsToPix(DEFAULT_LABEL_PADDING_TOP));
int rightPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingRight,
dipsToPix(DEFAULT_LABEL_PADDING_RIGHT));
int bottomPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingBottom,
dipsToPix(DEFAULT_LABEL_PADDING_BOTTOM));
mHint = a.getText(R.styleable.FloatLabelLayout_floatLabelHint);
mLabel = new TextView(context);
mLabel.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
mLabel.setVisibility(INVISIBLE);
mLabel.setText(mHint);
mLabel.setFocusable(true);
ViewCompat.setPivotX(mLabel, 0f);
ViewCompat.setPivotY(mLabel, 0f);
ApiCompatibilityUtils.setTextAppearance(mLabel,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
a.recycle();
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mInterpolator = AnimationUtils.loadInterpolator(context,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
? android.R.interpolator.fast_out_slow_in
: android.R.anim.decelerate_interpolator);
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
int leftPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingLeft,
dipsToPix(DEFAULT_LABEL_PADDING_LEFT));
int topPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingTop,
dipsToPix(DEFAULT_LABEL_PADDING_TOP));
int rightPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingRight,
dipsToPix(DEFAULT_LABEL_PADDING_RIGHT));
int bottomPadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelPaddingBottom,
dipsToPix(DEFAULT_LABEL_PADDING_BOTTOM));
mHint = a.getText(R.styleable.FloatLabelLayout_floatLabelHint);
mLabel = new TextView(context);
mLabel.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
mLabel.setVisibility(INVISIBLE);
mLabel.setText(mHint);
mLabel.setFocusable(true);
ViewCompat.setPivotX(mLabel, 0f);
ViewCompat.setPivotY(mLabel, 0f);
ApiCompatibilityUtils.setTextAppearance(mLabel,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
a.recycle();
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mInterpolator = AnimationUtils.loadInterpolator(context,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
? android.R.interpolator.fast_out_slow_in
: android.R.anim.decelerate_interpolator);
}
/**
* Work around some 'features' of TextView and URLSpans. i.e. vanilla URLSpans do not react to
* touch so we replace them with our own {@link TouchableUrlSpan}
* & {@link LinkTouchMovementMethod} to fix this.
* <p/>
* Setting a custom MovementMethod on a TextView also alters touch handling (see
* TextView#fixFocusableAndClickableSettings) so we need to correct this.
*/
public static void setTextWithNiceLinks(TextView textView, CharSequence input) {
textView.setText(input);
textView.setMovementMethod(LinkTouchMovementMethod.getInstance());
textView.setFocusable(false);
textView.setClickable(false);
textView.setLongClickable(false);
}