2013-01-02 219 views
0

我要填充在活動課這樣的字母按鍵...填充按鈕

enter image description here

我的代碼如下:

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); 
     } 

    } 

它導致該:

enter image description here

我可以人ually把所有的字母按鍵如下,但我認爲,在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" /> 

那麼,怎樣才能使用活動類的Java代碼轉換我上面的XML代碼。

請幫助。

謝謝。

+0

使用''GridView''沒有''RelativeLayout'' – 2013-01-02 06:43:25

+0

我怎樣才能插入網格內的按鈕? – newbie

+0

您也可以使用Gridview或TableLayout來做到這一點。 – itsrajesh4uguys

回答

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()

您可以通過OnClickListenerAlphabetButtonAdapter只需添加一個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); 

} 
+0

爲什麼是AlphabetButtonAdapter(上下文上下文,int資源,int textViewResourceId,列表對象)!= public Asa(Context context, int resource, int textViewResourceId, List objects) {? – newbie

+0

鍵盤故障,對不起:) – 2013-01-02 07:03:34

+0

我應該把什麼參數放在資源,textViewResourceId,對象? – newbie

1

你忘了ID的。按照此處所述設置按鈕的ID。

Setting up RelativeLayout in java code

這將使代碼:

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); 
    } 

}   
+0

這不起作用... – newbie

0

你應該使用的LinearLayout與水平方向和一個固定的寬度和重量參數,可以代替RelativeLayout的上面提到。試着改變你的活動佈局,也是你的代碼像波紋管:

私人無效populateButtons(){

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); 
    } 

}