英文:
Connecting buttons to class instances in Android
问题
public class Category {
public static final int ADD = 1;
public static final int MINUS = 2;
public static final int MULTIPLE = 3;
public static final int DIVIDE = 4;
private int id;
private String name;
public Category() {
}
public Category(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return getName();
}
}
public class FirstScreenActivity extends AppCompatActivity implements View.OnClickListener{
public static final String EXTRA_NUMBER = "com.example.application.example.EXTRA_NUMBER";
private Button buttonAdd;
private Button buttonMinus;
private Button buttonMultiple;
private Button buttonDivide;
private Button buttonDivide2;
public int no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMinus = (Button) findViewById(R.id.buttonMinus);
buttonMultiple = (Button) findViewById(R.id.buttonMultiple);
buttonDivide = (Button) findViewById(R.id.buttonDivide);
buttonDivide2 = (Button) findViewById(R.id.buttonDivide2);
buttonAdd.setOnClickListener(this);
buttonMinus.setOnClickListener(this);
buttonMultiple.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonDivide2.setOnClickListener(this);
}
public void openActivityChose() {
Intent intent = new Intent(this, ActivityChose.class);
intent.putExtra(EXTRA_NUMBER, no);
startActivity(intent);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonAdd:
no = Category.ADD;
openActivityChose();
break;
case R.id.buttonMinus:
no = Category.MINUS;
openActivityChose();
break;
case R.id.buttonMultiple:
no = Category.MULTIPLE;
openActivityChose();
break;
case R.id.buttonDivide:
no = Category.DIVIDE;
openActivityChose();
break;
case R.id.buttonDivide2:
no = 5; // Not sure what you want to associate with this button
openActivityChose();
break;
}
}
}
Please note that in the code above, I've associated each button with a specific value from the Category
class using the constants Category.ADD
, Category.MINUS
, Category.MULTIPLE
, and Category.DIVIDE
. This allows you to easily identify which button was clicked and pass the corresponding value to the next activity.
英文:
I've been working on simple android quiz app in java for counting. I've created class "category" that contains add,minus,multiple and divide and I want to connect each of those class instances to one button and I have problem doing this. Here's my code:
public class Category {
public static final int ADD = 1;
public static final int MINUS = 2;
public static final int MULTIPLE = 3;
public static final int DIVIDE = 4;
private int id;
private String name;
public Category() {
}
public Category(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return getName();
}
}
that's my Category^
public class FirstScreenActivity extends AppCompatActivity implements View.OnClickListener{
public static final String EXTRA_NUMBER = "com.example.application.example.EXTRA_NUMBER";
private Button buttonAdd;
private Button buttonMinus;
private Button buttonMultiple;
private Button buttonDivide;
private Button buttonDivide2;
public int no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMinus = (Button) findViewById(R.id.buttonMinus);
buttonMultiple = (Button) findViewById(R.id.buttonMultiple);
buttonDivide = (Button) findViewById(R.id.buttonDivide);
buttonDivide2 = (Button) findViewById(R.id.buttonDivide2);
buttonAdd.setOnClickListener(this);
buttonMinus.setOnClickListener(this);
buttonMultiple.setOnClickListener(this);
buttonDivide.setOnClickListener(this);
buttonDivide2.setOnClickListener(this);
}
public void openActivityChose() {
Intent intent = new Intent(this, ActivityChose.class);
intent.putExtra(EXTRA_NUMBER, no);
startActivity(intent);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonAdd:
no = 1;
openActivityChose();
break;
case R.id.buttonMinus:
no = 2;
openActivityChose();
break;
case R.id.buttonMultiple:
no = 3;
openActivityChose();
break;
case R.id.buttonDivide:
no = 4;
openActivityChose();
break;
case R.id.buttonDivide2:
no = 5;
openActivityChose();
break;
}
}
}
Here's activity ^
And my question is what do I have to put in each case to bind each class instance to correct button? Thanks in advance!
答案1
得分: 0
以下是您要翻译的内容:
您覆盖的 onClick 方法是活动的 onClick,而不是按钮的。
您需要为每个按钮单独设置 onClick 方法。
最好使用 findElementById() 进行操作。
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 在响应按钮点击时执行一些操作
}
});
英文:
The onClick method you override is the activity onClick instead of the button.
You need to set the method onClick for each button separately.
probably best by using findElementById()
From Android Docs
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论