【问题标题】:Android Declare buttons programmatically?Android以编程方式声明按钮?
【发布时间】:2012-03-14 05:57:03
【问题描述】:

在我的音板应用程序中,我有 80 个按钮,它们有一个点击监听器和一个长按监听器。

我的按钮在 xml 中声明为:

<TableRow
        android:id="@+id/tableRow1"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/sound0"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight=".31"
            android:longClickable="true"
            android:text="@string/sound0" >
        </Button>

        <Button
            android:id="@+id/sound1"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight=".31"
            android:longClickable="true"
            android:text="@string/sound1" >
        </Button>

        <Button
            android:id="@+id/sound2"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight=".31"
            android:longClickable="true"
            android:text="@string/sound2" >
        </Button>
    </TableRow>

并且监听器设置为:

Button SoundButton0 = (Button) findViewById(R.id.sound0);
    SoundButton0.getBackground().setAlpha(150);

    SoundButton0.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String name = getString(R.string.sound0);
            tracker.trackEvent("Clicks", "Play", name, 0);
            playSound(R.raw.sound0);

        }
    });
    SoundButton0.setOnLongClickListener(new OnLongClickListener() {

        public boolean onLongClick(View v) {
            String name = getString(R.string.sound0);
            tracker.trackEvent("Clicks", "Saved", name, 0);
            ring(soundArray[0], name);
            return false;

        }
    });

有没有一种方法可以在 for 循环中以编程方式完成所有这些操作,以便每个按钮的唯一更改是 SoundButtonx,其中每个按钮的 x 增加一。

【问题讨论】:

    标签: android


    【解决方案1】:

    是的,有一个明确的解决方案:

    Button[] buttons; 
    for(int i=0; i<buttons.length; i++) {
    {
         String buttonID = "sound" + (i+1);
    
         int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
         buttons[i] = ((Button) findViewById(resID));
         buttons[i].setOnClickListener(this);
    }
    

    注意:使用具有诸如 sound1、sound2、sound3、sound4....等 id 的按钮声明 XML 布局。

    对于同样的问题,这里有一个更明确的例子 => Android – findViewById() in a loop

    【讨论】:

    • 现在我如何设置一个点击监听器来捕捉每个按钮?
    【解决方案2】:

    是的,有。在你的 for 循环中,你首先声明一个 Button,并传递给它的构造函数一个上下文。然后设置每个按钮的布局参数。将每个按钮添加到父视图(在父视图上使用 addView 方法)。最后,使用activity的setContentView方法,将parent作为参数传递。

    【讨论】:

      【解决方案3】:
      yes take a look at this
      
      for(int x = 0;x<80;x++)
      {
      Button btn = new Button(this);
      btn.setlayoutParams(new LayoutParams(LayoutParams.wrap_content,LayoutParams.wrap_conmtent);
      btn.setId(100 + x);
      btn.setOnClickListener(this);
      btn.setOnlongClickListener(this);
      this.addView(btn);
      }
      
      //this will create 80 buttons and setlisteners on them
      
      //in your overrides of onclick and onLongClick identiffy them as 
      
          public void onClick(View v) {
              // TODO Auto-generated method stub
              int id = v.getId();
          if(id == 100 + 1 )
          {
      //your code
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 2011-03-07
        • 1970-01-01
        相关资源
        最近更新 更多