Create onClickListener case switch for TextView
I just got started in Java programming and I am having trouble implementing the switch body OnClickListener for my interactive text elements. I managed to make a toggle case for menu items, but I obviously don't understand enough to make a more general case.
Here are the pieces of my code that are important to him
public class MyActivity extends Activity implements SensorEventListener {
TextView tv, tv1, tv2, tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
//get textviews
tv = (TextView) findViewById(R.id.xval);
tv1 = (TextView) findViewById(R.id.yval);
tv2 = (TextView) findViewById(R.id.zval);
tv3 = (TextView) findViewById(R.id.scalar);
And then I set up an individual click listener for each TextView, for example.
tv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do things
}
}
});
But I'm trying to customize it, so I have a combined OnClickListener like:
@Override
public boolean onClickListener (View v) {
switch (tv.findViewById()) {
case tv:
//Do things
return true;
case tv1:
//Do things
return true;
case tv2:
//Do things
return true;
case tv3:
//Do things
return true;
}}
I know the code is very wrong, but I don't seem to wrap my head around it. I've already assigned my findViewById, so I'm not sure what else to add to the switch!
Thankyou!
source to share
I will give an alternative answer. First you need to create OnClickListener
one that will receive your OnClick events:
OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.xval:
//code
break;
case R.id.yval:
//code
break;
case R.id.zval:
//code
break;
case R.id.scalar:
//code
break;
default:
break;
}
}
};
Then you have to bind this listener to each TextView
one you have:
tv.setOnClickListener(listener);
tv1.setOnClickListener(listener);
tv2.setOnClickListener(listener);
tv3.setOnClickListener(listener);
As soon as you click one of the TextViews
callback will be called OnClickListener
onClick()
and it will check the id TextView
you clicked and run the code as appropriate, as appropriate.
source to share
tv.setOnClickListener(this);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
@Override
public boolean onClick (View v) {
switch (v.getId()) {
case R.id.xval:
//Do things
return true;
case R.id.yval:
//Do things
return true;
case R.id.zval:
//Do things
return true;
case R.id.scalar:
//Do things
return true;
}}
source to share
Create one listener, add it to all TextView
. Include a view id which is simpleint
View.OnClickListener listener = new View.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId()) {
case R.id.xval:
//Do things
return true;
case R.id.yval:
//Do things
return true;
case R.id.zval:
//Do things
return true;
case R.id.scalar:
//Do things
return true;
}
}
};
tv.setOnClickListener(listener);
tv1.setOnClickListener(listener);
tv2.setOnClickListener(listener);
tv3.setOnClickListener(listener);
source to share