There are 2 View elements and 2 Button elements. I want that by clicking on one Button text changes only in one View , and not in two, as is happening now.

I do not know how to divide it all in the code.

 public class MainActivity extends ActionBarActivity { private TextView mTextView; private TextView m2TextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.textView); m2TextView = (TextView) findViewById(R.id.textView2); } public void onClick(View view) { mTextView.setText("privet ot 1 view"); m2TextView.setText("Privet ot 2 view"); } } 
  • 2
    If you have been given the correct answer - vote for it \ check it correct (gray tick on the side of the answer). This will help others understand that the answer came up to solve the problem. Write in the comments - "thanks, works" should not be. - pavlofff

1 answer 1

Rewrite your processor as follows:

 public void onClick(View view) { switch (view.getId()) { case R.id.btn1: mTextView.setText("privet ot 1 view"); break; case R.id.btn2: m2TextView.setText("Privet ot 2 view"); break; } } 

Only R.id.btn1 and R.id.btn2 will replace the id their buttons

  • Thank you very much. everything is working. - Romik romikromik