按钮的 OnClick 更多视图应该在运行时添加到布局中,但我无法做到这一点

发布于 2024-10-18 16:21:22 字数 474 浏览 2 评论 0原文

我想通过单击按钮添加视图。我的屏幕有一个文本视图、一个编辑文本和一个“添加更多”按钮。单击按钮后,另一组文本视图和编辑文本应附加到屏幕上。我还计划在屏幕变长后添加滚动视图。 我的代码片段如下

   button.setOnClickListener(new OnClickListener() {  
        @Override  
    public void onClick(View v) {  
        text.setText("hello how are you?");  //this is just an example, I may want to add lot more here.
    linearLayout.addView(text);  
    setContentView(linearLayout);  
        }  

我知道我缺少一些非常基本的东西,但我不知道那是什么。 :(
请帮忙。

I want to add views on click of button. My screen has a text view and an edit text and a "Add More" button. on the click of button another set of text view and edit text should get appended to the screen. I am also planning to add scroll view once the my screen grows longer.
My Code snippet is as follows

   button.setOnClickListener(new OnClickListener() {  
        @Override  
    public void onClick(View v) {  
        text.setText("hello how are you?");  //this is just an example, I may want to add lot more here.
    linearLayout.addView(text);  
    setContentView(linearLayout);  
        }  

I know I am missing something very basic but I do not know what is that. :(
Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-10-25 16:21:22

首先,您需要使用对上下文的引用来初始化每个 TextView。因此,在您的 onClick 中,假设它位于 MyActivity.java 中:

public void onClick(View v) {
  TextView text = new TextView(this);
  text.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT); // Or what you decide
  text.setText("Your text");
  linearLayout.addView(text);
}

其次,假设您在 onCreate 中调用了 setContentView,这也会将您的 LinearLayout 添加到视图中,因此您不需要每次单击按钮时都调用 setContentView(linearLayout)被点击。

First of all, you need to initialize each TextView with a reference to the context. So, in your onClick, given that it is in MyActivity.java:

public void onClick(View v) {
  TextView text = new TextView(this);
  text.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT); // Or what you decide
  text.setText("Your text");
  linearLayout.addView(text);
}

Second, given that you have a call to setContentView in onCreate which also adds your linearlayout to the view, you do not need to call setContentView(linearLayout) each time the button is clicked.

与之呼应 2024-10-25 16:21:22

您应该首先创建一个新的 TextView 并使用 linearLayout.addView()EditText 添加到 linearLayout 中。无需调用 setContentView() 您可能已经添加了它。

you should first create a new TextView and EditText add it to the linearLayout using linearLayout.addView(). No need to call setContentView() you probably already added it.

颜漓半夏 2024-10-25 16:21:22

这一个可以帮助你

在你的 XML 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myMainRelativeLayout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="47dp"
    android:text="Add Button" />

和你的 Java 文件中像

public class MainActivity extends Activity implements
    android.view.View.OnClickListener {

Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnAdd) {
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
        Button btn = new Button(this);
        btn.setId(1234);
        btn.setText("Add Button Runtime");
        btn.setOnClickListener(this);
        rl.addView(btn);
    }
    if(v.getId() == 1234)
    {
        Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();

    }
}

}

这只是一个例子,你可以添加任何像这样的视图

This One help you

In your XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myMainRelativeLayout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="47dp"
    android:text="Add Button" />

And your Java file Like

public class MainActivity extends Activity implements
    android.view.View.OnClickListener {

Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnAdd) {
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
        Button btn = new Button(this);
        btn.setId(1234);
        btn.setText("Add Button Runtime");
        btn.setOnClickListener(this);
        rl.addView(btn);
    }
    if(v.getId() == 1234)
    {
        Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();

    }
}

}

This one just example you can add any view like this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文