在Android EditText中以编程方式获取提示文本值

6

我需要动态更改EditText的字体,并且在字体更改时处理提示文本。我按照以下步骤进行 :

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_layout_textBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/textBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_date" />
</android.support.design.widget.TextInputLayout>

我的代码片段如下:

EditText textBox = (EditText) findViewById(R.id.textBox);
Typeface tf = Typeface.createFromAsset(this, "myfont.ttf");
textBox.setTypeface(tf);
String hintText = textBox.getHint().toString(); //Got NullPointerException here....
String newText = processText(hintText); //This will process the hint text;
textBox.setHint(newText);

当我运行程序时,出现了以下异常:
java.lang.NullPointerException: 尝试在空对象引用上调用接口方法'java.lang.String java.lang.CharSequence.toString()'
请帮我解决这个问题。

你尝试过getParent().getHint()吗?你可能正在使用设计支持库。 - Anis LOUNIS aka AnixPasBesoin
@Anix PasBeson,EditText中没有getParent()。getHint()的方法。 - Thiha Zaw
请发布您的 XML。 - Anis LOUNIS aka AnixPasBesoin
@Anix PasBeson,我已经编辑了我的XML代码片段。 - Thiha Zaw
@Anix PasBesoin。是的,我已经尝试过了,现在没问题了。非常感谢! - Thiha Zaw
1个回答

2
我们需要先查看your_layout.xml文件来确定,但您可能正在使用设计支持库。
如果是这样,您可以按照以下方式获取提示:
((TextInputLayout)textBox.getParent()).getHint();

注意

这个问题已经在这里得到了回答。 (尽管我们需要确保您正在使用设计支持库来将您的问题标记为重复)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接