android 里 怎么给多个button加上监听器?????
publicclassGuessNumberActivityextendsActivity{/**Calledwhentheactivityisfirstcreated....
public class GuessNumberActivity extends Activity {
/** Called when the activity is first created. */
int ButtonNum = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout main = (LinearLayout)findViewById(R.id.main);
TableLayout table = (TableLayout)findViewById(R.id.tablelayout);
TableRow tablerow;
Button[] BotArray = new Button[81];
for(int i = 0;i<9;i++)
{
tablerow = new TableRow(this);
createButton(i,tablerow,table,BotArray);
}
}
public void createButton(int guess_number,TableRow tablerow,TableLayout table,final Button[] BotArray)
{
Button bn1;
for(int i = 0;i<9;i++)
{
int randomNum=(int)( Math.random()*10);
bn1 = new Button(this);
bn1.setTextSize((float)17.0);
bn1.setText(randomNum+" ");
bn1.setMaxHeight(5);
BotArray[ButtonNum] = bn1;
ButtonNum++;
tablerow.addView(bn1);
}
table.addView(tablerow);
}
} 展开
/** Called when the activity is first created. */
int ButtonNum = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout main = (LinearLayout)findViewById(R.id.main);
TableLayout table = (TableLayout)findViewById(R.id.tablelayout);
TableRow tablerow;
Button[] BotArray = new Button[81];
for(int i = 0;i<9;i++)
{
tablerow = new TableRow(this);
createButton(i,tablerow,table,BotArray);
}
}
public void createButton(int guess_number,TableRow tablerow,TableLayout table,final Button[] BotArray)
{
Button bn1;
for(int i = 0;i<9;i++)
{
int randomNum=(int)( Math.random()*10);
bn1 = new Button(this);
bn1.setTextSize((float)17.0);
bn1.setText(randomNum+" ");
bn1.setMaxHeight(5);
BotArray[ButtonNum] = bn1;
ButtonNum++;
tablerow.addView(bn1);
}
table.addView(tablerow);
}
} 展开
展开全部
给多个button加上监听器,使用一个for循环
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//do what you want
}
});
通过onclick方法里面的参数哦View v这个对象的 getID(),可以拿到这个button的id,然后通过
switch 进行判断,哪个button应当做出什么样的处理!
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//do what you want
}
});
通过onclick方法里面的参数哦View v这个对象的 getID(),可以拿到这个button的id,然后通过
switch 进行判断,哪个button应当做出什么样的处理!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String[] names = Config.names;
i =0;
for (String name : names)
{
i++;
Button button =new Button(this);
button.setText(name);
button.setBackgroundResource(R.drawable.button);
button.setOnClickListener(new Button.OnClickListener()
{
@Override
publicvoid onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent =new Intent(Main.this,
Other.class);
myApp.setI(i);
startActivity(intent);
}
});
layout.addView(button);
}
i =0;
for (String name : names)
{
i++;
Button button =new Button(this);
button.setText(name);
button.setBackgroundResource(R.drawable.button);
button.setOnClickListener(new Button.OnClickListener()
{
@Override
publicvoid onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent =new Intent(Main.this,
Other.class);
myApp.setI(i);
startActivity(intent);
}
});
layout.addView(button);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询