簡體   English   中英

如何使onClickListener for Android數組按鈕工作

[英]How to get onClickListener for Android array of buttons to work

我創建了一個來自sqllite數據庫的按鈕動態數組。 我一生都無法設置點擊偵聽器來分別處理每個按鈕。 我一直在尋找3個小時但沒有成功。

任何幫助,將不勝感激。 不用擔心數據庫廢話,它只是偵聽器。

imports...

public class CreatePlayList extends Activity {

    ScrollView scrollleft, scrollright;
    LinearLayout songsright, songsleft;
    TextView testtext;
    String[][] allsongs;
    int numofsongs, i;
    Button b1[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createplaylist);
        scrollleft = (ScrollView) findViewById(R.id.scrollleft);
        scrollright = (ScrollView) findViewById(R.id.scrollright);
        songsright = (LinearLayout) findViewById(R.id.layRight);
        songsleft = (LinearLayout) findViewById(R.id.layLeft);
        LyricDb connect = new LyricDb(CreatePlayList.this);
        connect.open();
        numofsongs = connect.getNumSongs();
        b1 = new Button[(numofsongs)];
        allsongs = new String[numofsongs][2];
        allsongs = connect.getSongArray();
        connect.close();
        testtext = new TextView(this);
        testtext.setText("Test");
        songsleft.addView(testtext);

        for (i = 0; i < allsongs.length; i++) {
            b1[i] = new Button(this);
            b1[i].setText(allsongs[i][1]);
            songsright.addView(b1[i]);
        }

        b1[19].setText("test 123");
        createClic();
    }

    public void createClic(){
        for (i = 0; i < (allsongs.length - 1); i++) {
            b1[i].setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    testtext.setText(b1[i].getText());
                }
            }); 
        } 
    }
}

轉儲在班級聲明的'i'。 用這個替換方法(“按鈕theButton”需要保持'最終')。

public void createClic()
{
    for (int i = 0; i < (allsongs.length - 1); i++)
    {
        final Button theButton = b1[i];
        theButton.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                testtext.setText(theButton.getText());
            }
        });
    }
}

首先在Activity中實現OnClickListener,並在創建時將setOnClickListener()方法調用到所有按鈕。

b[i].setOnClickListener(this);
and also set id with each button 
b[i].setId(i);  

//and override this method
@Override
public void onClick(View v) 
{
    //here check which button called
    if(v.getId==1)
    {
        //its Button 1  do what ever u want     
    }
    if(v.getId==2)
    {
        //its Button 2  do what ever u want its Button 2
    }
    ......
}

嘗試一次......

b1[i] = new Button[allsongs.length];
  for (i = 0; i < allsongs.length; i++) {

        b1[i].setText(allsongs[i][1]);
        songsright.addView(b1[i]);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM