我已经在 android activity.java 文件中以编程方式创建了 edittexts。我想为编辑文本添加文本输入布局。有什么办法可以做到这一点。我已将 edittext 包装在 xml 文件中。我希望对以编程方式创建的编辑文本执行相同的操作。
提前致谢。
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_input_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 txtInputLayout = (TextInputLayout) LayoutInflater.from(getActivity()).inflate(R.layout.text_input_layout,null);
TextInputEditText txtInputEditText = (TextInputEditText) txtInputLayout.findViewById(R.id.text_input_edit_text);
mRoot.addView(txtInputLayout);
关于android - 以编程方式创建的编辑文本的文本输入布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36811369/