0

我想像这样在活动类中填充字母按钮......

在此处输入图像描述

我的代码如下:

private void populateButtons() {
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelativeLayout01);
        for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++) {
            String buttonText = String.valueOf(buttonChar);
            Button alphabetButton = new Button(this);
            alphabetButton.setText(buttonText);
            layout.addView(alphabetButton);
        }
        
    }

结果是这样的:

在此处输入图像描述

我可以手动将所有字母按钮按如下方式放置,但我认为在 Activity 类中对其进行编码更好:

    <Button
        android:id="@+id/button1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/violet"
        android:text="A"
        android:textColor="@color/main"
        android:textSize="20dp"
        android:onClick="methodHere" />

那么如何使用 Activity 类中的 java 代码转换上述 xml 代码。

请帮忙。

谢谢你。

4

3 回答 3

1

你可以试试这个。

private void populateButtons() {

        ArrayAdapter<String> chars = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);

        GridView layout = (GridView) findViewById(R.id.gridView);
        for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++) {
            String buttonText = String.valueOf(buttonChar);
            chars.add(buttonText);
        }
        layout.setAdapter(chars);
    }

如果您真的想插入按钮,请创建一个扩展类Adapter

ArrayAdapter足够。

public class AlphabetButtonAdapter extends ArrayAdapter<String>
{

        private List<String> objects;
        public AlphabetButtonAdapter(Context context,
                int textViewResourceId,
                List<String> objects)
        {
            super(context, textViewResourceId, objects);
            this.objects = objects;

        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            Button b = new Button(getContext());
            b.setText(objects.get(position));
            return b;
        }

        public void setOnClickListener(OnClickListener listener)
        {
             this.listener = listener;
        }

}

使用自定义适配器填充

private void populateButtons() {


            ArrayList<String> list = new ArrayList<String>();            
            GridView layout = (GridView) findViewById(R.id.gridView);
            for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++) {
                String buttonText = String.valueOf(buttonChar);
                list.add(buttonText);
            }

            AlphabetButtonAdapter chars = new AlphabetButtonAdapter(context, 0, list);
            layout.setAdapter(chars);

        }

更新1:绑定onClick()按钮

您可以传入OnClickListener只需AlphabetButtonAdapter添加一个 setter 方法

public class MyActivity OnClickListener
{

    @Override
    public void onClick(View v)
    {
             Button b = (Button) v;
             // Your definition...

    }
}

private void populateButtons() {


                ArrayList<String> list = new ArrayList<String>();            
                GridView layout = (GridView) findViewById(R.id.gridView);
                for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++) {
                    String buttonText = String.valueOf(buttonChar);
                    list.add(buttonText);
                }

                AlphabetButtonAdapter chars = new AlphabetButtonAdapter(context, 0, list);
                chars.setOnClickListener(listener);
                layout.setAdapter(chars);

}
于 2013-01-02T06:48:53.253 回答
1

你忘记了身份证。为这里讨论的按钮设置 ID。

在java代码中设置RelativeLayout

这将使代码:

private void populateButtons(){

  RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelativeLayout01); 

  for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++){

        String buttonText = String.valueOf(buttonChar);
        Button alphabetButton = new Button(this);
        alphabetButton.setId('A');// This is just an unique integer.
        alphabetButton.setText(buttonText);
        layout.addView(alphabetButton);
    }

}        
于 2013-01-02T06:54:29.613 回答
0

您应该使用具有水平方向和固定 Width 和 Weight 参数的 LinearLayout,而不是上面提到的 RelativeLayout。尝试更改您的活动布局以及您的代码,如下所示:

私人无效填充按钮(){

    LinearLayout layout = (LinearLayout) findViewById(R.id.RelativeLayout01);

    for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++) {
        String buttonText = String.valueOf(buttonChar);
        Button alphabetButton = new Button(this);
        alphabetButton.setText(buttonText);
        layout.addView(alphabetButton);
    }

}
于 2013-08-11T14:02:19.097 回答