以编程方式添加TextInputLayout
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以编程方式添加TextInputLayout相关的知识,希望对你有一定的参考价值。
我如何以编程方式添加TextInputLayout?
我试过了,
private EditText _editText;
private TextInputLayout _textInputLayout;
// Create the EditText
if (_editText == null) {
_editText = new EditText(this.getContext());
_editText.setInputType(
_isPassword ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
);
_editText.setLayoutParams(
new LayoutParams(0, LayoutParams.MATCH_PARENT)
);
if (_hint != null) {
_editText.setHint(String.format("%s%s", HINT_PREFIX, _hint.toLowerCase()));
}
_textInputLayout = new TextInputLayout(this.getContext());
_textInputLayout.setLayoutParams(
new LayoutParams(0, LayoutParams.WRAP_CONTENT, EDIT_TEXT_WEIGHT)
);
_textInputLayout.addView(_editText);
this.addView(_textInputLayout);
}
但是,当我编译并运行时,我得到以下错误。我在这做错了什么?
10-03 21:22:16.154 2488-2488/? E/androidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=54; index=108
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.content.res.TypedArray.getValueAt(TypedArray.java:1132)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.content.res.TypedArray.getColor(TypedArray.java:446)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.support.design.widget.CollapsingTextHelper.setCollapsedTextAppearance(CollapsingTextHelper.java:191)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.support.design.widget.TextInputLayout.setHintTextAppearance(TextInputLayout.java:270)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:111)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:82)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:78)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.syncopatesystems.splogle.iconfloatedittext.IconFloatEditText.initialize(IconFloatEditText.java:145)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.nixgadgets.iconfloatedittext.IconFloatEditText.<init>(IconFloatEditText.java:75)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.nixgadgets.iconfloatedittext.IconFloatEditText.<init>(IconFloatEditText.java:62)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:619)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.Activity.setContentView(Activity.java:2166)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.syncopatesystems.splogle.activities.SignUpWithEmailActivity.onCreate(SignUpWithEmailActivity.java:43)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-03 21:22:16.154 2488-2488/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案
EditText editText = new EditText(this);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
TextInputLayout textInputLayout = new TextInputLayout(this);
LinearLayout.LayoutParams textInputLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
textInputLayout.setLayoutParams(textInputLayoutParams);
textInputLayout.addView(editText, editTextParams);
textInputLayout.setHint("hint");
setContentView(textInputLayout);
另一答案
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class TextClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// RealtiveLayout
// First you create an RelativeLayout which will hold the
// TextInputLayout
RelativeLayout relativeLayout;
relativeLayout = new RelativeLayout(this);
// Set generated ID
relativeLayout.setId(View.generateViewId());
// Define RelativeLayout layoutParams
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
// Set the layoutParams to the relativeLayout
relativeLayout.setLayoutParams(relativeLayoutParams);
// EditText
// Instantiate EditText view which will be held inside of
// TextInputLayout
EditText editText = new EditText(this);
// Add an ID to it
editText.setId(View.generateViewId());
// Get the Hint text for EditText field which will be presented to the
// user in the TextInputLayout
editText.setHint(field.getHint());
// Set color of the hint text inside the EditText field
editText.setHintTextColor(getResources().getColor(android.R.color.white));
// Set the font size of the text that the user will enter
editText.setTextSize(16);
// Set the color of the text inside the EditText field
editText.setTextColor(getResources().getColor(android.R.color.white));
// Define layout params for the EditTExt field
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Set editText layout parameters to the editText field
editText.setLayoutParams(editTextParams);
/*
* Next, you do the same thing for the TextInputLayout (instantiate,
* generate and set ID, set layoutParams, set layoutParamt for
* TextInputLayout
*/
// TextInputLayout
TextInputLayout textInputLayout = new TextInputLayout(this);
textInputLayout.setId(View.generateViewId());
RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
textInputLayout.setLayoutParams(textInputLayoutParams);
// Then you add editText into a textInputLayout
textInputLayout.addView(editText, editTextParams);
// Lastly, you add the textInputLayout into (or onto) the layout you've
// chosen.
relativeLayout.addView(textInputLayout, textInputLayoutParams);
setContentView(relativeLayout);
}
}
另一答案
结束使用MaterialEditText库。这比我遇到的大多数其他库有更多的功能。
以上是关于以编程方式添加TextInputLayout的主要内容,如果未能解决你的问题,请参考以下文章
使用 TextInputLayout 时更改 EditText 提示颜色