There is 1 activity, in it 2 buttons, 1 TextView, 2 string resources. When you press the button 1, the text from it (the string resource of the button 1) gets and is set to TextView; when you press the button 2, everything is the same (only the string resource is different). For simplicity, while divided into two buttons. In the future, the button will be one; after clicking, it will change its string text resource and, depending on the resource, it will be installed in texView.

View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Button btn = (Button)v; String btnText = btn.getText().toString(); if (btnText.equals("Left")) { tv.setText(R.string.btn_left); } else if (btnText.equals("Right")) { tv.setText(R.string.btn_right); } else { Toast toast = Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT); toast.show(); } } }; 

Tried to get through getResources (). GetString (), but something did not work. While the text in .equals () is set hard, because Pushing there R.string.btn_left / right does not work.

I did it and it worked:

 public class MainActivity extends Activity implements View.OnClickListener { Button btn_left; Button btn_right; TextView tv; String leftStr; String rightStr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.textView); btn_left = (Button) findViewById(R.id.button); btn_left.setOnClickListener(this); btn_right = (Button) findViewById(R.id.button2); btn_right.setOnClickListener(this); leftStr = getString(R.string.btn_left); rightStr = getString(R.string.btn_right); } @Override public void onClick(View v) { Button btn = (Button)v; String btnText = btn.getText().toString(); if (btnText.equals(leftStr)) { tv.setText(R.string.btn_left); } else if (btnText.equals(rightStr)) { tv.setText(R.string.btn_right); } else { Toast toast = Toast.makeText(MainActivity.this, "Wrong 1", Toast.LENGTH_SHORT); toast.show(); } } } 

Any comments on this code?

  • one
    If you are in activation, you can get a string from the resources just getString(R.string.app_name) But it is not entirely clear what exactly does not work for you? - Android Android
  • Slightly rewrote the code. It is necessary to ensure that the onClick method worked depending on the text resource that is now attached to the button. - morethanfish
  • 1) Do you have OnClickListener attached to only two buttons? Why do I just do if? tv.setText (btn.getText (). toString ()); for that matter. 2) If you click it, you will tie it to another twist (for example, go to implement click on text) you will have a program with an error of ClassCastSpection, since it will not be able to convert the twist to Button. What I need to do as it is accepted and sooner or later you will still come to the conclusion that in OnClick you will take the id views and, based on this, perform the required action. - Roman Novoselov

2 answers 2

It is necessary by the identifier of a string resource to get a string and transmit it already, and not an identifier

  getString(R.string.btn_right) 

And install in the TextView already the line

  tv.setText(getString(R.string.btn_right)); 

Specifically, in your case, I would be attached to the id of the button, why should I twist the view to the button, take the text from the button and compare it. We look at which button was pressed and set the corresponding line in Textview:

  View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: tv.setText(MyActivity.this.getResources().getString(R.string.string1)); break; case R.id.button2: // делаешь то, что нужно сделать при тыке на кнопку 2 break; default: // все остальные вью, для которых установлен данный листенер } } }; 

Again, it is not accepted to create a separate listener, it is better to implement OnClickListener interface in Activiti.

  public class MyActivity extends AppCompatActivity implements View.OnClickListener 

Well, implement the method OnClick

  @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: // делаешь то, что нужно сделать при тыке на кнопку 1 break; case R.id.button2: // делаешь то, что нужно сделать при тыке на кнопку 2 break; default: // все остальные вью, у кого в качестве листенера установлена данное активити } } 

It will remain as a button to pick up the activation

 button1.setOnClickListener(this); button2.setOnClickListener(this); 
  • Yes, I thought about it that way, but the fact is that later there will be one button, and it will be necessary to change the text, depending on the string resource on the button. - morethanfish
  • Well, and the string resource on the button depends on what? From some variable most likely! What prevents this variable from being processed in the listener and outputting text depending on it? - Roman Novoselov
  • Making a condition on the basis of a string resource is sorry for some kind of IMHO - Roman Novoselov
  • I fixed all the code above, look and express your IMHO) - morethanfish

You do a switch on a line, correct on

 switch(v.getId()) 
  • This would be suitable if I tried to determine which button was pressed, and so, one button - 2 string resources. - morethanfish