Android 以编程方式创建的EditText的文本输入布局
我已经在android activity.java文件中以编程方式创建了EditText。我想为EditText添加textinputlayout。有没有办法做到这一点。我已将edittext包装到xml文件中。我希望对以编程方式创建的EditText也这样做 提前谢谢Android 以编程方式创建的EditText的文本输入布局,android,android-support-library,android-textinputlayout,Android,Android Support Library,Android Textinputlayout,我已经在android activity.java文件中以编程方式创建了EditText。我想为EditText添加textinputlayout。有没有办法做到这一点。我已将edittext包装到xml文件中。我希望对以编程方式创建的EditText也这样做 提前谢谢 public class FirstFragment extends Fragment { EditText emailText; EditText passText; Button lo
public class FirstFragment extends Fragment {
EditText emailText;
EditText passText;
Button loginButton;
TextView registerView;
int counter=0;
public FirstFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
emailText = (EditText) view.findViewById(R.id.emailText);
passText = (EditText) view.findViewById(R.id.passwordText);
loginButton = (Button) view.findViewById(R.id.LoginButton);
registerView = (TextView) view.findViewById(R.id.RegisterView);
final TextInputLayout nameText = new TextInputLayout(getActivity());
final TextInputLayout DeptText = new TextInputLayout(getActivity());
final EditText phoneNumber = new EditText(getActivity());
final Button registerButton = new Button(getActivity());
final LinearLayout layout = (LinearLayout)view.findViewById(R.id.fragmentLayout);
phoneNumber.setInputType(InputType.TYPE_CLASS_PHONE);
LinearLayout.LayoutParams parameters1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) ;
nameText.setLayoutParams(parameters1);
DeptText.setLayoutParams(parameters1);
phoneNumber.setLayoutParams(parameters1);
LinearLayout.LayoutParams parameters2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) ;
parameters2.gravity = Gravity.CENTER;
registerButton.setLayoutParams(parameters2);
nameText.setHint("Enter Name");
DeptText.setHint("Enter Department");
phoneNumber.setHint("Enter Phone Number");
registerButton.setText("Register");
registerView.setTextColor(Color.BLUE);
registerView.setTextSize(17);
registerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if(counter==1) {
layout.addView(nameText,1 );
layout.addView(DeptText, 2);
layout.addView(phoneNumber,3);
layout.removeView(loginButton);
layout.addView(registerButton,5);
registerView.setText("Already signed Up? login");
}
else{
layout.removeView(nameText);
layout.removeView(DeptText);
layout.removeView(phoneNumber);
layout.removeView(registerButton);
layout.addView(loginButton,2);
registerView.setText("New User? Sign Up");
counter=0;
}
}
});
return view;
}
}
您可以从XML文件中展开布局,如下所示: 在布局资源中创建text\u input\u layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:inputType="phone"
android:hint="change hint"
android:id="@+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
尝试将TextInputLayout看作EditText字段的包装器,例如:
LinearLayout assessmentLayout = (LinearLayout) findViewById(R.id.assessmentsLayout);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
assert assessmentLayout != null;
TextView screenTitle = new TextView(this);
screenTitle.setLayoutParams(layoutParams);
screenTitle.setText(String.format("Assessment: %s", course.getName()));
assessmentLayout.addView(screenTitle);
TextInputLayout titleWrapper = new TextInputLayout(this);
titleWrapper.setLayoutParams(layoutParams);
titleWrapper.setHint("Assessment Title:");
assessmentLayout.addView(titleWrapper);
title = new EditText(this);
title.setLayoutParams(layoutParams);
title.setText(assessment.getTitle());
titleWrapper.addView(title);
TextInputLayout descriptionWrapper = new TextInputLayout(this);
descriptionWrapper.setLayoutParams(layoutParams);
descriptionWrapper.setHint("Description:");
assessmentLayout.addView(descriptionWrapper);
description = new EditText(this);
description.setLayoutParams(layoutParams);
description.setText(assessment.getDescription());
descriptionWrapper.addView(description);
您可以将EditText视图添加到TextInputLayout包装器中,而不是直接添加到活动布局中。立即创建
TextInputLayout
的实例,然后在其上调用addView()
以添加EditText
。已尝试。它不起作用。编辑文本在运行时不会显示,请提供一个演示您的问题的示例。“我希望对以编程方式创建的编辑文本执行相同的操作”--您仅以编程方式创建一个EditText
(phoneNumber
)。您没有在TextInputLayout
上调用addView()
,而是传入phoneNumber
。您的其他EditText
小部件来自膨胀的布局资源;把你的TextInputLayout
包装器放在同一个布局资源中。我正在努力尝试,但无法理解。你能举个例子说明一下吗?谢谢。
LinearLayout assessmentLayout = (LinearLayout) findViewById(R.id.assessmentsLayout);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
assert assessmentLayout != null;
TextView screenTitle = new TextView(this);
screenTitle.setLayoutParams(layoutParams);
screenTitle.setText(String.format("Assessment: %s", course.getName()));
assessmentLayout.addView(screenTitle);
TextInputLayout titleWrapper = new TextInputLayout(this);
titleWrapper.setLayoutParams(layoutParams);
titleWrapper.setHint("Assessment Title:");
assessmentLayout.addView(titleWrapper);
title = new EditText(this);
title.setLayoutParams(layoutParams);
title.setText(assessment.getTitle());
titleWrapper.addView(title);
TextInputLayout descriptionWrapper = new TextInputLayout(this);
descriptionWrapper.setLayoutParams(layoutParams);
descriptionWrapper.setHint("Description:");
assessmentLayout.addView(descriptionWrapper);
description = new EditText(this);
description.setLayoutParams(layoutParams);
description.setText(assessment.getDescription());
descriptionWrapper.addView(description);