简体   繁体   English

在一个布局屏幕中创建多个(26)按钮

[英]Creating multiple(26) buttons in one layout screen

Trying to create a simple ABCs and 123s app. 尝试创建一个简单的ABC和123s应用程序。 My initial layout has two buttons, 1 for alphabet and one for numbers. 我的初始布局有两个按钮,一个用于字母,一个用于数字。 If alphabet button is clicked, I want to go to the next fragment which would hold 26 buttons for all alphabet (AZ). 如果单击字母按钮,我想转到下一个片段,该片段将为所有字母(AZ)保留26个按钮。 For numbers, 10 buttons (0-9). 对于数字,使用10个按钮(0-9)。 MY question is, how do i efficiently populate a layout file with 26 buttons/10 buttons without writing each one of them in the XML file? 我的问题是,如何有效地用26个按钮/ 10个按钮填充布局文件,而不将每个按钮都写入XML文件?

You can programmatically add buttons to a view. 您可以以编程方式向视图添加按钮。 Try this in your onCreateView function of you fragment. 在片段的onCreateView函数中尝试此操作。

//mView is your fragment root, and container is defined in XML
LinearLayout container = (LinearLayout)mView.findViewById(R.id.alpha_container)

String[] letters = {A, B, C, ..., Z};

for(int i=0; i<letters.length; i++) { 
   Button letter_btn = new Button(getActivity());
   letter.setText(letters[i]);
   container.addView(letter_btn, i);
}

This will programmatically create a button for each element in the letters array. 这将以编程方式为letter数组中的每个元素创建一个按钮。 You can attach onClickListener(s) accordingly and track the buttons by text content or give them a tag/id. 您可以相应地附加onClickListener并按文本内容跟踪按钮或为它们提供标签/ ID。

use gridview 使用GridView

activity xml 活动xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/gridview"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"
   android:columnWidth="90dp"
   android:numColumns="auto_fit"
   android:verticalSpacing="10dp"
   android:horizontalSpacing="10dp"
   android:stretchMode="columnWidth"
   android:gravity="center"
/>

your activity 你的活动

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      GridView gridview = (GridView) findViewById(R.id.gridview);
      gridview.setAdapter(new GridAdapter (this));
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

gridView adapter gridView适配器

public class GridAdapter extends BaseAdapter {
   private Context mContext;

   // Constructor
   public GridAdapter (Context c) {
      mContext = c;
   }

   public int getCount() {
      return alphabets.length;
   }

   public Object getItem(int position) {
      return null;
   }

   public long getItemId(int position) {
      return 0;
   }

   // create a new TextView for each item referenced by the Adapter
   public View getView(final int position, View convertView, ViewGroup parent) {
      TextView txView;

      if (convertView == null) {
         txView= new TextView (mContext);
         txView.setLayoutParams(new GridView.LayoutParams(85, 85));
         txView.setPadding(8, 8, 8, 8);
      } 
      else 
      {
         txView= (TextView) convertView;
      }
      txView.setText(alphabets[position]);

       **Update**
       txView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
            //your code here
               switch(position){
                case 0:
                    Toast.makeText(this,"A",Toast.LENGTH_LONG).show();
                break;
                case 1:
                    Toast.makeText(this,"B",Toast.LENGTH_LONG).show();
                break;
                ........SoOn
               }

           }});


      return txView;
   }

   //  array
   public String[] alphabets= {
      "A","B","C","D"
   };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM