开发者

How to getText of button

开发者 https://www.devze.com 2023-03-16 21:29 出处:网络
I have an array of buttons. Now, when each button is clicked, I want the text of buttons to be displayed in the textBox. I tried it but its not working. Here\'s my code:

I have an array of buttons. Now, when each button is clicked, I want the text of buttons to be displayed in the textBox. I tried it but its not working. Here's my code:

public boolean initDay()
    {
        LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
        LinearLayout rowLayout=null;

        LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);

        for (i = 0; i<6; i++) 
        {
            rowLayout = new LinearLayout(this);
            rowLayout.setWeightSum(7);
            layoutVertical.addView(rowLayout,param);
            for(j=0;j<7;j++)
            {
                pBtnDay[i][j]=new Button(this);
                rowLayout.addView(pBtnDay[i][j],param);
                pBtnDay[i][j].setClickable(true);
                pBtnDay[i][j].setOnClickListener(this);
            }
        }
        return true;
    }
    public void onClick(View v)
    {
         TextView t = (TextView)findViewById(R.id.textV);   
       开发者_开发百科  t.setText(pBtnDay[i][j].getText());
    }


You should change t.setText(pBtnDay[i][j].getText()); to t.setText(v.getText()); Variables i and j are not visible inside the onClick method and they are equal to 0 both in there.


public boolean initDay()
    {
        LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
        LinearLayout rowLayout=null;

        LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);

        for (i = 0; i<6; i++) 
        {
            rowLayout = new LinearLayout(this);
            rowLayout.setWeightSum(7);
            layoutVertical.addView(rowLayout,param);
            for(j=0;j<7;j++)
            {
                pBtnDay[i][j]=new Button(this);
                rowLayout.addView(pBtnDay[i][j],param);
                pBtnDay[i][j].setClickable(true);
                pBtnDay[i][j].setOnClickListener({
                public void onClick(View v){
TextView t = (TextView)findViewById(R.id.textV);   
         t.setText(view.getText());

});
            }
        }
        return true;
    }


public boolean initDay()
    {
        LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
        LinearLayout rowLayout=null;
        Button pBtnDay;
        LayoutParams param = new 
          TextView t = (TextView)findViewById(R.id.textV);
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
    for (i = 0; i<6; i++) 
    {
        rowLayout = new LinearLayout(this);
        rowLayout.setWeightSum(7);
        layoutVertical.addView(rowLayout,param);
        for(j=0;j<7;j++)
        {
            pBtnDay=new Button(this);
            rowLayout.addView(pBtnDay,param);
            pBtnDay.setClickable(true);

        }
    }
    return true;
}

pBtnDay.setOnClickListener(new OnClickListener(){

public void onClick(View v)
{

     t.setText(v.getText());
}
});
0

精彩评论

暂无评论...
验证码 换一张
取 消