[英]Android Studio - Create an EditText with a click of a button
Can't seem to find a post/video on the net that explains adding new EditText fields with a button. 似乎无法在网上找到可以解释使用按钮添加新EditText字段的帖子/视频。 I need to use the edittexts later.
我以后需要使用edittexts。 Can someone please explain to me how to create this system?
有人可以向我解释如何创建此系统吗? Or link a video/post that explains this.
或链接解释此问题的视频/帖子。 I've been searching for a long time but I still haven't found a good explanation.
我已经搜寻了很长时间,但仍然找不到很好的解释。 Thanks.
谢谢。
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText t = new EditText(myContext);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
root.addView(t);
}
});
root: is the root layout where you want to add the EditText. root:是您要在其中添加EditText的根布局。
use below code 使用以下代码
Add this Java File.. 添加此Java文件。
LinearLayout linearLayout = findViewById(R.id.editTextContainer);
Button btnShow = findViewById(R.id.btnShow);
if (btnShow != null) {
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create EditText
final EditText editText = new EditText(this);
editText.setHint(R.string.enter_something);
editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setPadding(20, 20, 20, 20);
// Add EditText to LinearLayout
if (linearLayout != null) {
linearLayout.addView(editText);
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.