单击并填充动态创建n个按钮

时间:2016-01-23 15:17:42

标签: android button dynamic buttonclick

单击按钮上的每个动态创建似乎都不起作用。

我目前有一个包含20个名字的列表,所以我想为每个名称生成20个按钮作为字符串列表中的名称我想用这些名称填充这20个按钮。

从这20个按钮我将点击并有一系列子名称,再次显示为按钮,但这些将根据第一次点击而变化。

我想这样做,因为名单会随着时间的推移而扩展,而不是从头开始按下每个按钮,系统会告诉系统有多少个按钮并加载每个按钮的名称。< / p>

  1. 我需要一个循环并设置数字20,因此它会生成20个按钮。 id为buttonname1到按钮名称20。

  2. 使用相关文本生成的按钮填充按钮1到20。

  3. 我需要一个带有按钮模板的布局,以便生成时按钮看起来一样。非常简单但我发现的例子都没有意义或有效。

  4. 如果有人能指出我这方面的有用教程,我将非常感激,同时我会继续搜索。

    如果一开始我可以生成20个带有20个ID的按钮,那就可以了。

    由于

3 个答案:

答案 0 :(得分:1)

  
      
  1. 我需要一个循环并设置数字20,因此它会生成20个按钮。 id为   buttonname1到按钮名称20.
  2.   
  3. 使用按钮1填充关联文本生成的按钮   到20岁。
  4.   

您需要考虑使用ListViewRecyclerView。这些视图包含任意数量的数据,并且一次只显示几个项目。在您的情况下,项目将是按钮,其文本是您要显示的名称。

为每个按钮附加一个监听器,用于定义单击时需要执行的操作。您需要查看膨胀视图并动态添加它们。

这就是它的全部内容。关于这些问题以及大量的SO问题都有很好的文档。

  
      
  1. 我需要一个带按钮模板的布局,以便在生成时按钮看起来一样。很简单,但我找到的例子都没有   感觉或工作。
  2.   

您需要了解样式和主题。定义按钮的XML时,可以在其中设置样式。由于您反复使用相同的XML,所有按钮看起来都是一样的。

答案 1 :(得分:1)

编辑:您需要的是动态创建视图。如果您确实需要一个按钮作为视图,那么以编程方式添加它们。

Button button = new Button(this); // example
  
      
  1. 我需要一个循环并设置数字20,因此它会生成20个按钮。 id为buttonname1到按钮名称20。
  2.   
  3. 使用相关文本生成的按钮填充按钮1至20。
  4.   
    String[] names = {"John","Jack",...,"Someone"}; // 20 names
    Button[] buttons = new Button[20];
    for (int i = 0; i < 20; i++) {
        Button button = new Button(this);
        button.setId("buttonname"+(i+1));
        button.setText(names[i]);
        buttons[i] = button;
    }

然后,您可以将阵列中的按钮添加到布局中,看起来应该是这样的。只需确保将布局方向设置为垂直,以便按钮将堆叠在每个按钮的顶部。你可以搜索一下。

    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
    for (int i = 0; i < 20; i++) {
        layout.addView(buttons[i]);
    }
  
      
  1. 我需要一个带按钮模板的布局,以便在生成时按钮看起来一样。非常简单但我发现的例子都没有意义或有用。
  2.   

您可以参考this tutorial设置所需的更多属性。您可以搜索“以编程方式添加Android视图”,“在Android中创建动态视图”等关键字。使用编程和动态一词。希望这有助于你的情况。

答案 2 :(得分:0)

好的,今天我第二次得到了我需要的东西(恢复它),只需要弄清楚下一步该做什么,但这是我的代码。

我的布局主要.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
</RelativeLayout>

我的活动。基于网络上的动态按钮在安德鲁德。http://alexanderoorayil.blogspot.co.uk/2012/05/dynamic-button-in-android.html

 package com.example.dynamic_buttons;


 import android.app.Activity;

 import android.graphics.Typeface;

 import android.os.Bundle;

 import android.view.View;

 import android.view.ViewGroup.LayoutParams;

 import android.widget.Button;

 import android.widget.CheckBox;

 import android.widget.CompoundButton;

 import android.widget.CompoundButton.OnCheckedChangeListener;

 import android.widget.LinearLayout;

 import android.widget.ScrollView;

 import android.widget.Toast;


public class MyActivity extends Activity {

Button b;

ScrollView scrollview;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

  //String Manuf;

    scrollview = new ScrollView(this);

    LinearLayout linearlayout = new LinearLayout(this);

    linearlayout.setOrientation(LinearLayout.VERTICAL);

    scrollview.addView(linearlayout);



    for(int i = 0; i<28;i++)

    {

        LinearLayout linear1 = new LinearLayout(this);

        linear1.setOrientation(LinearLayout.HORIZONTAL);

        linearlayout.addView(linear1);

        b = new Button(this);

        int id = getResources().getIdentifier("Manuf"+i, "string", getPackageName());
        String Manuf = getResources().getString(id);

        b.setText(Manuf);

        b.setId(i);

        b.setTextSize(30);

        b.setPadding(0,0,0,0);

       // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);

        b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));



        linear1.addView(b);



       /* CheckBox checkbox = new CheckBox(this);

        checkbox.setId(i);

        checkbox.setText("Wow..");

        linear1.addView(checkbox);



        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {



            @Override

            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "Checked.."+ arg0.getId(), Toast.LENGTH_SHORT).show();

            }

        });
    */


        b.setOnClickListener(new View.OnClickListener() {



            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "Yipee.."+ v.getId(), Toast.LENGTH_SHORT).show();

            }

        });

    }

    this.setContentView(scrollview);

}

}

我的strings.xml文件中的字符串启动Manfu0 -28。

在没有你的帮助和评论的情况下无法完成它,希望我将学习如何使用,因为我现在需要为按下按钮的某些ID创建一些动作。

由于