我想在TextInputLayout中添加一个新的EditText,就像它们在xml文件中静态添加一样。 (所以在开始时有两个TextInputLayouts,当用户按下按钮时,它应该添加新的)。我看到了其他问题,我知道它与父母布局有关,但我无法使其发挥作用。顺便说一句,我使用绑定。在我的情况下,我认为父母应该是ScrollView。对于EditText,我尝试了RelativeLayout和LinearLayout,但在任何情况下都会启动错误。
这里是xml文件。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView">
<RelativeLayout
android:id="@+id/activity_multi_opt_poll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xyz.MyApp.EntryActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/nameInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name">
<android.support.design.widget.TextInputEditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/questionInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameInputLayout"
android:hint="Question">
<android.support.design.widget.TextInputEditText
android:id="@+id/questionEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/opt1InputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/questionInputLayout"
android:hint="1. Option">
<android.support.design.widget.TextInputEditText
android:id="@+id/opt1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/opt2InputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/opt1InputLayout"
android:hint="2. Option">
<android.support.design.widget.TextInputEditText
android:id="@+id/opt2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:fabSize="normal"
app:srcCompat="@android:drawable/ic_input_add"
android:id="@+id/addFAB"
android:layout_marginEnd="21dp"
android:layout_below="@+id/opt2InputLayout"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</ScrollView>
</layout>
添加新功能
public class EntryActivity extends AppCompatActivity {
private ActivityEntryBinding binding;
private List optionsViews = new ArrayList();
private int count = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
binding = DataBindingUtil.setContentView(this, R.layout.activity_entry);
binding.addFAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.scrollView.addView(createNewOptionEntry());
}
});
}
private TextInputLayout createNewOptionEntry() {
ScrollView.LayoutParams lparams = new ScrollView.LayoutParams(
ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.MATCH_PARENT);
TextInputLayout textInputLayout = new TextInputLayout(this);
textInputLayout.setLayoutParams(lparams);
textInputLayout.setHint(count++ + ". Option");
LinearLayout.LayoutParams lparams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextInputEditText editText = new TextInputEditText(this);
editText.setLayoutParams(lparams2);
int id = View.generateViewId();
/*to be able to get dynamically generated editText's input*/
optionsViews.add(id);
editText.setId(id);
textInputLayout.addView(editText, lparams);
return textInputLayout;
}
}
在行textInputLayout.addView(editText,lparams)给我错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: mattoncino.pollo, PID: 23572
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at android.support.design.widget.TextInputLayout.updateInputLayoutMargins(TextInputLayout.java:371)
at android.support.design.widget.TextInputLayout.addView(TextInputLayout.java:271)
at android.view.ViewGroup.addView(ViewGroup.java:3540)
at mattoncino.pollo.MultiOptPollActivity.createNewOptionEntry(MultiOptPollActivity.java:95)
at mattoncino.pollo.MultiOptPollActivity.access$000(MultiOptPollActivity.java:27)
at mattoncino.pollo.MultiOptPollActivity$1.onClick(MultiOptPollActivity.java:48)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
好的,现在我认为我解决了布局问题。 1)TextInputLayout的父级有FrameLayout,我从父级获取它。 2)EditText的父级是TextnputLayout,它是LinearLayout的子类,因此EditText应将其布局设置为LinearLayout。 我觉得这部分还可以。但现在它给了我一个错误,说ScrollView只能托管一个直接孩子。所以我应该将它实际添加到RelativeLayout?
Type of 'exoPlayer' doesn't match the type of the overridden var-property 'public abstract var exoPlayer: ExoPlayer? defined in...