我已經在android activity.java文件中以編程方式創建了edittexts。我想爲edittext添加textinputlayout。有沒有辦法做到這一點。我已將edittext包裝在xml文件中。我希望對以編程方式創建的edittext執行相同的操作。text以編程方式創建的edittexts的輸入佈局
在此先感謝。
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;
}
}
關閉袖口,創建一個'TextInputLayout'的實例,然後調用'addView()'來添加'EditText'。嘗試了 – CommonsWare
。它不工作。編輯文本在運行時不顯示 –
然後請提供一個[mcve]來演示您的問題。 – CommonsWare