- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在前 Lollipop 设备的样式中使用 Lollipop 的新功能,例如 coloraccent、colorPrimary。
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/material_teal500</item>
<item name="colorPrimaryDark">@color/material_teal600</item>
<item name="colorAccent">@color/material_white</item>
</style>
现在我想为我的 TextView 、编辑文本和按钮创建自定义字体。 在样式中,我将 coloraccent 用作白色。所以在编辑文本的焦点上应该出现白色。请参阅下图了解默认的 edittext 焦点。这很好用。
但每当我创建自定义字体编辑文本时,焦点行为就会不同。 它不显示白色。相反,它以黑色显示。请参阅下图了解我的电子邮件自定义编辑文本行为。
这是我自定义的 edittext 字体类。
public class TypefaceButton extends Button {
/*
* Permissible values for the "typeface" attribute.
*/
private final static int ROBOTO_THIN = 0;
private final static int ROBOTO_THIN_ITALIC = 1;
private final static int ROBOTO_LIGHT = 2;
private final static int ROBOTO_LIGHT_ITALIC = 3;
private final static int ROBOTO_REGULAR = 4;
private final static int ROBOTO_ITALIC = 5;
private final static int ROBOTO_MEDIUM = 6;
private final static int ROBOTO_MEDIUM_ITALIC = 7;
private final static int ROBOTO_BOLD = 8;
private final static int ROBOTO_BOLD_ITALIC = 9;
private final static int ROBOTO_BLACK = 10;
private final static int ROBOTO_BLACK_ITALIC = 11;
private final static int ROBOTO_CONDENSED = 12;
private final static int ROBOTO_CONDENSED_ITALIC = 13;
private final static int ROBOTO_CONDENSED_BOLD = 14;
private final static int ROBOTO_CONDENSED_BOLD_ITALIC = 15;
/**
* List of created typefaces for later reused.
*/
private final static SparseArray<Typeface> mTypefaces = new SparseArray<Typeface>(16);
/**
* Simple constructor to use when creating a view from code.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public TypefaceButton(Context context) {
super(context);
}
/**
* Constructor that is called when inflating a view from XML. This is called
* when a view is being constructed from an XML file, supplying attributes
* that were specified in the XML file. This version uses a default style of
* 0, so the only attribute values applied are those in the Context's Theme
* and the given AttributeSet.
* <p/>
* <p/>
* The method onFinishInflate() will be called after all children have been
* added.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @see #TypefaceButton(android.content.Context, android.util.AttributeSet, int)
*/
public TypefaceButton(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributes(context, attrs);
}
/**
* Perform inflation from XML and apply a class-specific base style. This
* constructor of View allows subclasses to use their own base style when
* they are inflating.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyle The default style to apply to this view. If 0, no style
* will be applied (beyond what is included in the theme). This may
* either be an attribute resource, whose value will be retrieved
* from the current theme, or an explicit style resource.
* @see #TypefaceButton(android.content.Context, android.util.AttributeSet)
*/
public TypefaceButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseAttributes(context, attrs);
}
/**
* Parse the attributes.
*
* @param context The Context the view is running in, through which it can access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
*/
private void parseAttributes(Context context, AttributeSet attrs) {
TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView);
int typefaceValue = values.getInt(R.styleable.TypefaceTextView_typeface, 0);
values.recycle();
setTypeface(obtaintTypeface(context, typefaceValue));
}
/**
* Obtain typeface.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param typefaceValue values for the "typeface" attribute
* @return Roboto {@link android.graphics.Typeface}
* @throws IllegalArgumentException if unknown `typeface` attribute value.
*/
private Typeface obtaintTypeface(Context context, int typefaceValue) throws IllegalArgumentException {
Typeface typeface = mTypefaces.get(typefaceValue);
if (typeface == null) {
typeface = createTypeface(context, typefaceValue);
mTypefaces.put(typefaceValue, typeface);
}
return typeface;
}
/**
* Create typeface from assets.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param typefaceValue values for the "typeface" attribute
* @return Roboto {@link android.graphics.Typeface}
* @throws IllegalArgumentException if unknown `typeface` attribute value.
*/
private Typeface createTypeface(Context context, int typefaceValue) throws IllegalArgumentException {
Typeface typeface;
switch (typefaceValue) {
case ROBOTO_THIN:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Thin.ttf");
break;
case ROBOTO_THIN_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-ThinItalic.ttf");
break;
case ROBOTO_LIGHT:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
break;
case ROBOTO_LIGHT_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf");
break;
case ROBOTO_REGULAR:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
break;
case ROBOTO_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf");
break;
case ROBOTO_MEDIUM:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf");
break;
case ROBOTO_MEDIUM_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-MediumItalic.ttf");
break;
case ROBOTO_BOLD:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
break;
case ROBOTO_BOLD_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldItalic.ttf");
break;
case ROBOTO_BLACK:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
break;
case ROBOTO_BLACK_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BlackItalic.ttf");
break;
case ROBOTO_CONDENSED:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Condensed.ttf");
break;
case ROBOTO_CONDENSED_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-CondensedItalic.ttf");
break;
case ROBOTO_CONDENSED_BOLD:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensed.ttf");
break;
case ROBOTO_CONDENSED_BOLD_ITALIC:
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf");
break;
default:
throw new IllegalArgumentException("Unknown `typeface` attribute value " + typefaceValue);
}
return typeface;
}
}
我在 xml 中使用自定义字体,如下所示。
<appname.utilities.TypefaceEditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="@dimen/ten_dp"
custom:typeface="roboto_light"
android:ems="10"
android:hint="@string/user_email"
android:inputType="textEmailAddress"
android:textColor="@color/material_grey200"
android:textColorHint="@color/material_grey200" />
我该如何解决这个问题!任何的想法?我知道我可以在 java 类文件中实现自定义字体。但是我想以这种方式实现。在我自己的自定义编辑文本中,我也想显示相同的强调色。在此先致谢!
最佳答案
我找到了!要应用 colorAccent,只需扩展您的自定义 EditText 类android.support.v7.widget.AppCompatEditText
关于android - 自定义字体 EditText 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26814324/
/** Called when the user clicks the Send button */ public void sendMessage(View view) { Intent i
我已经编写了 EditText 的子类。这是那个子类: package com.package.foo; import android.content.Context; import android.
我有一个用户名和密码 XML 段 我希望当用户名字段到达第9个字符时光标焦点自动跳转到密码字段 我在看 Focus next textview automatically并且正在尝试: fin
我知道这类问题被问过很多次。但仍然没有人给出完美的答案。 我有问题: 我想从 EditText1 ** 移动到另一个 **EditText2 。我已经检测到editText1,但如何将光标移动到edi
我有 2 个 EditText(myEditText1 和 myEditText2) 我需要触摸 myEditText1 1.做一些生意[这里没有问题,需要申请的业务] 2. 然后请求焦点到 myEd
我想在用户输入 EditText 时将文本放入我的应用程序中的某个字符串中,并使用它在 Activity 中生动地显示它(在不同的 View 中......) - 就像谷歌的实时/即时搜索一样...
当我将文本插入到我的 EditText 字段时,文本本身与 EditText 的行之间存在异常间隙。这是我终端的打印屏幕,您可以在其中看到我正在谈论的这个差距,它被标记为红色。 我试过文本对齐和引力但
我的应用程序需要帮助。这是我的相对布局的 xml 文件,我需要在按下 Enter 键后跳转到下一个编辑文本。但是有一个问题,如果我正在写入 editTextKm 并按 Enter 键,它会跳转到 ed
Hello All 我已经将 onLongClickListener() 用于我的 EditText View ,但是每当我长时间单击 View 时,就会出现一个弹出窗口,不允许我执行我的操作onLo
EditText 快把我们逼疯了!真的! 我们的应用程序登录屏幕出现问题。它包含两个 EditText,但只有第一个(用户名)获得焦点,通过 SoftKeyboard 仅在触摸/单击用户名 EditT
如何在kotlin中获取editText并用toast显示。 var editTextHello = findViewById(R.id.editTextHello) 我试过了,但显示对象 Toast
我一直在浏览各种线索来解决这个问题,但我仍然感到困惑。当我将“adjustPan”应用于底部具有 EditText 的 Activity 时,UI 会正确向上推,但编辑文本会被底部的键盘稍微遮挡。经过
我有两个类:a 和 b。在类 a 中,我有一个线程,当变量 x 的值小于 1000 时,它会将变量 x 加一。在类 b(Activity 类)中,我有一个名为 ed 的 EditText。每当a类中x
今天我遇到了一个非常有趣的情况。 假设: EditText 从 XML 实例化。 EditText editText; editText = findViewByID(R.id.editT
有谁知道我的代码出了什么问题,我正在尝试比较编辑文本字段中的输入,以确保它们在创建帐户之前具有相同的值。 //compare the fields contents if(editT
制作一个简单的卡路里转换器。 这是图片中的问题。 我使用的是 Genymotion 模拟器。 这是我遇到问题的屏幕部分: 当我单击 EditText 数字字段时,数字键盘会出现 但是我必须单击下一个箭
全部 - 我试图隐藏 EditText B、C,直到 EditText A 中至少有一个字符。我尝试过使用文本观察器... EditText editText = (EditText) findVie
我正在努力完成 “Sam 的 24 小时 Android 应用程序开发技术”,但在某些时候我的昵称和电子邮件设置停止正确保存。它现在根本不保存昵称并将电子邮件保存到两者。我做了什么导致这个,我该如何解
我有一个 SharedPreferences,它保存来自一个 Activity 的 EditText 输入并在另一个 Activity 中显示字符串值。 当我在 EditText 字段中输入内容并按下
这一定是一个我可能会再次忽略的简单问题。这是我的问题我的 string.xml 上有一个字符串数组 Alabama Florida Los Angeles Virgi
我是一名优秀的程序员,十分优秀!