Java Android Studio-点击按钮创建编辑文本
似乎在网上找不到解释如何使用按钮添加新EditText字段的帖子/视频。我以后需要使用编辑文本。有人能给我解释一下如何创建这个系统吗?或者链接一个视频/帖子来解释这一点。我已经找了很长时间了,但仍然没有找到一个好的解释。谢谢Java Android Studio-点击按钮创建编辑文本,java,android,button,android-edittext,Java,Android,Button,Android Edittext,似乎在网上找不到解释如何使用按钮添加新EditText字段的帖子/视频。我以后需要使用编辑文本。有人能给我解释一下如何创建这个系统吗?或者链接一个视频/帖子来解释这一点。我已经找了很长时间了,但仍然没有找到一个好的解释。谢谢 Button mButton = (Button) findViewById(R.id.my_button); mButton.setOnClickListener(new OnClickListener() { @Override public void
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);
}
});
根:是要添加编辑文本的根布局
根:是要添加编辑文本的根布局。使用下面的代码
添加此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);
}
}
});
}
使用下面的代码
添加此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);
}
}
});
}
你好欢迎使用stack overflow,如果您的问题没有得到答案,请查看以获得有关如何在stack overflow中改进您的问题的好提示;)你已经看过这个帖子了吗?我猜你已经搜索了很长一段时间了,但是这个网站上似乎有很多现有的问答可以回答你的问题:在google中输入“用按钮添加新的EditText字段:stackoverflow.com”表明帖子和许多其他潜在的相关问题。还是这个?发现的代码似乎都不起作用,因为我最后总是出错。我想要的是有人解释它是如何工作的。回答你的问题,是的,我有。嗨!欢迎使用stack overflow,如果您的问题没有得到答案,请查看以获得有关如何在stack overflow中改进您的问题的好提示;)你已经看过这个帖子了吗?我猜你已经搜索了很长一段时间了,但是这个网站上似乎有很多现有的问答可以回答你的问题:在google中输入“用按钮添加新的EditText字段:stackoverflow.com”表明帖子和许多其他潜在的相关问题。还是这个?发现的代码似乎都不起作用,因为我最后总是出错。我想要的是有人解释它是如何工作的。回答你的问题,是的,我有。