如何在Android中动态创建的按钮上设置随机文本?

时间:2014-01-30 09:54:06

标签: android button

首先抱歉英语不好,这里我附加了屏幕截图,我创建了第一行的动态按钮,秒行的按钮静态来自xml,我可以成功创建按钮现在(1)我想要当我点击下面行的任何按钮,我想在动态创建的第一个第一个按钮上设置文本,在我的代码中,它设置在最后一个按钮上,(2)如果第一个按钮有一些文本而不是文本将设置为第二个按钮,我使用下面的代码。

private Button Dynamic_Button;

    layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
    for (int i = 1; i < 6; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        Dynamic_Button = new Button(this);
        Dynamic_Button.setId(i);

        final int id_ = Dynamic_Button.getId();
        layout.addView(Dynamic_Button, params);
        Dynamic_Button = ((Button) findViewById(id_));
        Dynamic_Button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(),
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();

            }
        });
    }

//以及动态按钮

上设置文字的代码
Dynamic_Button.setText(str); 

enter image description here

3 个答案:

答案 0 :(得分:1)

将其保存在一个集合中,例如ArrayList,并在按下Button

时以正确的索引检索对象

答案 1 :(得分:1)

由于您使用的for循环

,因此设置在最后一个按钮上

您必须创建一个按钮数组,而不是仅使用一个Button Object

试试这个 创建一个类变量

Button[] Dynamic_Button = new Button[5];

layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
    for (int i = 1; i < 6; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        Dynamic_Button[i-1] = new Button(this);
        Dynamic_Button[i-1].setId(i);

        final int id_ = Dynamic_Button[i-1].getId();
        layout.addView(Dynamic_Button[i-1], params);
        //This is not necessary
        //Dynamic_Button[i-1] = ((Button) findViewById(id_));
        Dynamic_Button[i-1].setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(),
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();

            }
        });
    }

并在settext中使用此逻辑

public void setTextToDynamicButton(String text){
    for (int i = Dynamic_Button.length - 1; i > 0; i--) {
        if(i-1 >= 0){
        Dynamic_Button[i].setText(Dynamic_Button[i-1].getText().toString());
        }
    }
    Dynamic_Button[0].setText(text);
}

答案 2 :(得分:1)

检查此代码 - 经过试用和测试

public class MainActivity extends Activity {

private Button dynamic_Button;
private LinearLayout layout = null;
private Button add = null;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
    add = (Button) findViewById(R.id.Add);

    for (int i = 1; i < 6; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        dynamic_Button = new Button(this);
        dynamic_Button.setId(i);
        final int id_ = dynamic_Button.getId();
        layout.addView(dynamic_Button, params);
        dynamic_Button = ((Button) findViewById(id_));
        dynamic_Button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(),
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();

            }
        });
    }

    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            for (int i = 1; i < 6; i++)
            {
                if(((Button)findViewById(i)).getText().toString().trim().length()<=0)
                {
                    ((Button)findViewById(i)).setText(""+i);
                    break;
                }
            }
        }
    });
}
}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rel_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linear_layout_tags"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
</LinearLayout>

<LinearLayout
    android:id="@+id/linear_static_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linear_layout_tags" >

    <Button
        android:id="@+id/Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add" />
</LinearLayout>

</RelativeLayout>