可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I\'m currently making a simple calculator app on Android. Im trying to set up the code so that when a number button is pressed it updates the calculator screen with that number. Currently I\'m doing it like this.
Button one = (Button) findViewById(R.id.oneButton);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView output = (TextView) findViewById(R.id.output);
output.append(\"1\");
}
});
It works but I\'m writing this same code for every single button on the calculator. As you can imagine it is very redundant. Is there anyway I can write this code in a more efficient way? One that involves not writing this method for every single button?
回答1:
You Just Simply have to Follow these steps for making it easy...
You don\'t have to write new onClickListener
for Every Button
... Just Implement View.OnClickLister
to your Activity
/Fragment
.. it will implement new Method called onClick()
for handling onClick Events for Button
,TextView` etc.
- Implement
OnClickListener()
in your Activity
/Fragment
public class MainActivity extends Activity implements View.OnClickListener {
}
- Implement onClick() method in your Activity/Fragment
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
// default method for handling onClick Events..
}
}
- Implement
OnClickListener()
For Buttons
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Button one = (Button) findViewById(R.id.oneButton);
one.setOnClickListener(this); // calling onClick() method
Button two = (Button) findViewById(R.id.twoButton);
two.setOnClickListener(this);
Button three = (Button) findViewById(R.id.threeButton);
three.setOnClickListener(this);
}
- Find Buttons By Id and Implement Your Code..
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.oneButton:
// do your code
break;
case R.id.twoButton:
// do your code
break;
case R.id.threeButton:
// do your code
break;
default:
break;
}
}
Please refer to this link for more information :
https://androidacademic.blogspot.in/2016/12/multiple-buttons-onclicklistener-android.html
This will make easier to handle many buttons click events and makes it looks simple to manage it...
回答2:
You could set the property:
android:onClick=\"buttonClicked\"
in the xml file for each of those buttons, and use this in the java code:
public void buttonClicked(View view) {
if (view.getId() == R.id.button1) {
// button1 action
} else if (view.getId() == R.id.button2) {
//button2 action
} else if (view.getId() == R.id.button3) {
//button3 action
}
}
回答3:
I created dedicated class that implements View.OnClickListener.
public class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, \"Button Clicked\", Toast.LENGTH_SHORT).show();
}
}
Then, I created an instance of this class in MainActivity
private ButtonClickListener onClickBtnListener = new ButtonClickListener();
and then set onClickListener for button
btn.setOnClickListener(onClickBtnListener);
回答4:
Set a Tag on each button to whatever you want to work with, in this case probably an Integer. Then you need only one OnClickListener for all of your buttons:
Button one = (Button) findViewById(R.id.oneButton);
Button two = (Button) findViewById(R.id.twoButton);
one.setTag(new Integer(1));
two.setTag(new Integer(2));
OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView output = (TextView) findViewById(R.id.output);
output.append(v.getTag());
}
}
one.setOnClickListener(onClickListener);
two.setOnClickListener(onClickListener);
回答5:
public class MainActivity extends AppCompatActivity implements OnClickListener {
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) findViewById(R.id.button);
b2= (Button) findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button)
{
Intent intent=new Intent(getApplicationContext(),SignIn.class);
startActivity(intent);
}
else if (v.getId()==R.id.button2)
{
Intent in=new Intent(getApplicationContext(),SignUpactivity.class);
startActivity(in);
}
}
}
回答6:
you can do like this
TextView output = (TextView) findViewById(R.id.output);
one.setOnClickListener(youractivity.this);
// set the onclicklistener for other buttons also
@Override
public void onClick(View v) {
int id = v.getId();
switch(id) {
case R.id.oneButton:
append(\"1\",output);
break;
case R.id.twoButton:
append(\"2\",output);
break;
case R.id.threeButton:
append(\"3\",output);
break;
}
}
private void append(String s,TextView t){
t.setText(s);
}
you can identify the views in your activity in a seperate method.
Hope this will help you.
回答7:
I had a similar issue and what I did is:
Create a array of Buttons
Button buttons[] = new Button[10];
Then to implement on click listener and reference xml id\'s I used a loop like this
for (int i = 0; i < 10; i++) {
String buttonID = \"button\" + i;
int resID = getResources().getIdentifier(buttonID, \"id\",
\"your package name here\");
buttons[i] = (Button) findViewById(resID);
buttons[i].setOnClickListener(this);
}
But calling them up remains same as in Prag\'s answer point 4.
PS- If anybody has a better method to call up all the button\'s onClick, please do comment.
回答8:
Implement onClick() method in your Activity/Fragment
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (itemId) {
// if you call the fragment with nevigation bar then used.
case R.id.nav_menu1:
fragment = new IntroductionFragment();
break;
// if call activity with nevigation bar then used.
case R.id.nav_menu6:
Intent i = new Intent(MainActivity.this, YoutubeActivity.class);
startActivity(i);
// default method for handling onClick Events..
}
}
回答9:
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.button1:
//do ur code
Toast.makeText(this,\"This is button 1\",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Intent intent = new Intent(this,SecondActivity.class);
Intent.putExtra(\"key\",value);
startActivity(intent);
break;
case R.id.button3:
//do ur code
break;
case R.id.button4:
//do ur code;
break;
default:
//do ur code;
}
}