如何知道哪个按钮是第一个在10个按钮的点击 [英] How to know which button is first clicked within 10 buttons an
问题描述
我创建一个游戏在Android中,其中将有10拖着它的用户必须记住,然后单击来回1-10一一编号。 我想知道的是那里的Android知道哪个按钮是第一次单击任何函数或方法,那么我可以设置这样的values..thankyou。 我试图让一个函数,将计算按钮的编号单击了,然后增加其价值,以配合我的按钮:
code ::
公共无效settingFirst pressedButton(INT按钮pressed)
{
如果(第一pressedButton == -1)//当第一次pressedButton = -1时表示无按键是pressed
第一pressedButton =按钮pressed;
}
然后:
b1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
如果(第一pressedButton == - 1){
如果(b1.getText()==1){
b1.setBackgroundColor(Color.GREEN);
打破;
}}
其他 {
如果(第一pressedButton == 0){
如果(b1.getText()==2){
b1.setBackgroundColor(Color.GREEN);
打破;
}
}
其他 {
如果(第一pressedButton == 1){
如果(b1.getText()==3){
b1.setBackgroundColor(Color.GREEN);
打破;
}}
其他 {
如果(第一pressedButton == 2){
如果(b1.getText()==4){
b1.setBackgroundColor(Color.GREEN);
打破;
}
}
其他 {
如果(第一pressedButton == 3){
如果(b1.getText()==5){
b1.setBackgroundColor(Color.GREEN);
打破;
}
}
其他 {
如果(第一pressedButton == 4){
如果(b1.getText()==6){
b1.setBackgroundColor(Color.GREEN);
打破;
}
}
其他 {
如果(第一pressedButton == 5){
如果(b1.getText()==6){
b1.setBackgroundColor(Color.GREEN);
打破;
}
}
其他 {
如果(第一pressedButton == 6){
如果(b1.getText()==7){
b1.setBackgroundColor(Color.GREEN);
打破;
}
}
其他 {
如果(第一pressedButton == 7){
如果(b1.getText()==8){
b1.setBackgroundColor(Color.GREEN);
打破;
}
其他 {
如果(第一pressedButton == 8){
如果(b1.getText()==9){
b1.setBackgroundColor(Color.GREEN);
打破;
}}
其他 {
如果(第一pressedButton == 9){
如果(b1.getText()==10){
b1.setBackgroundColor(Color.GREEN);
打破;
您可以使用自己的活动
为 OnClickListener
。例如
公共类MyActivity扩展活动实现View.OnClickListener {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
F1 =(按钮)findViewById(R.id.f1);
F2 =(按钮)findViewById(R.id.f2);
:
:
f1.setOnClickListener(本);
f2.setOnClickListener(本);
:
:
:
}
}
您必须实现在view.getId()onClick的方法和开关:
@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.f1:
//你的点击动作
打破;
案例R.id.f2:
//你的点击动作
打破;
:
:
}
}
如果你想按钮数组,那么
按钮btns [] =新的按钮[10];
的for(int i = 0;我小于10;我++){
btns [我] =新的按钮(这一点);
btns [I] .setText(+ I);
}
如果你希望所有的人都这样做,你可以使用一个for循环遍历这样的数组一样的东西:
的for(int i = 0; I< btns.length;我++){
btns [I] .setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
//做一点事
Log.d(键,值+ btns [I] .getText();
}
});
}
I am creating a game in android where there will be 10 shuffled numbers which user have to memorize and then click fro 1-10 one by one. I want to know is there any function or way in android to know which button is first clicked,then i can set that way the values..thankyou. I was trying to make a function that will count the number of button clicked then increase its value to match with my buttons:
CODE::
public void settingFirstPressedButton (int buttonPressed)
{
if (firstPressedButton == -1) //when firstPressedButton = -1 it means no button was pressed
firstPressedButton = buttonPressed;
}
then:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(firstPressedButton==-1){
if(b1.getText()=="1"){
b1.setBackgroundColor(Color.GREEN);
break;
}}
else {
if(firstPressedButton==0){
if(b1.getText()=="2"){
b1.setBackgroundColor(Color.GREEN);
break;
}
}
else {
if(firstPressedButton==1){
if(b1.getText()=="3"){
b1.setBackgroundColor(Color.GREEN);
break;
} }
else {
if(firstPressedButton==2){
if(b1.getText()=="4"){
b1.setBackgroundColor(Color.GREEN);
break;
}
}
else {
if(firstPressedButton==3){
if(b1.getText()=="5"){
b1.setBackgroundColor(Color.GREEN);
break;
}
}
else {
if(firstPressedButton==4){
if(b1.getText()=="6"){
b1.setBackgroundColor(Color.GREEN);
break;
}
}
else {
if(firstPressedButton==5){
if(b1.getText()=="6"){
b1.setBackgroundColor(Color.GREEN);
break;
}
}
else {
if(firstPressedButton==6){
if(b1.getText()=="7"){
b1.setBackgroundColor(Color.GREEN);
break;
}
}
else {
if(firstPressedButton==7){
if(b1.getText()=="8"){
b1.setBackgroundColor(Color.GREEN);
break;
}
else {
if(firstPressedButton==8){
if(b1.getText()=="9"){
b1.setBackgroundColor(Color.GREEN);
break;
}}
else {
if(firstPressedButton==9){
if(b1.getText()=="10"){
b1.setBackgroundColor(Color.GREEN);
break;
you can use your own Activity
as OnClickListener
. For instance
public class MyActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f1 =(Button) findViewById(R.id.f1);
f2 =(Button) findViewById(R.id.f2);
:
:
f1.setOnClickListener(this);
f2.setOnClickListener(this);
:
:
:
}
}
you have to implement the onClick method and switch upon view.getId():
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.f1:
// your click action
break;
case R.id.f2:
// your click action
break;
:
:
}
}
if you want array of buttons then
Button btns[]=new Button[10];
for (int i = 0; i < 10; i++) {
btns[i] = new Button(this);
btns[i].setText(""+i);
}
If you want all of them to do the same thing you could use a for loop to loop over the array like this:
for(int i = 0; i< btns.length; i++){
btns[i].setOnClickListener(new OnClickListener() {
public void onClick(View v){
//do something
Log.d("Button click","Value " + btns[i].getText() ;
}
});
}
这篇关于如何知道哪个按钮是第一个在10个按钮的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!