Issue
I just want to implement one button to do multiple actions like on first click make Textview1 visible and on second click make Textview2 visible and so on.
here is my code it works but for 2 actions only i want to set more visible component in one button i hope its clear and Thanks for any help
final TextView textView_r4 = findViewById(R.id.tv_r4);
final EditText editText_r4 = findViewById(R.id.input_R4);
final TextView textView_r5 = findViewById(R.id.tv_r5);
final EditText editText_r5 = findViewById(R.id.input_R5);
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView_r4.setVisibility(View.VISIBLE);
editText_r4.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.Addbtn).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
textView_r5.setVisibility(View.VISIBLE);
editText_r5.setVisibility(View.VISIBLE);
return true;
}
});
Solution
You can add an enum State to keep track of which state your button is in. Create a class field in the same class (activity) that these methods are in, and change the state every time you click. Then in the .setOnClickListener method you can check which state the button is in, and depending on that do different actions.
private State state = INITIAL;
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (state) {
case INITIAL:
// do first action
state = State.CLICKED_ONCE;
break;
case CLICKED_ONCE:
// do second action
state = State.CLICKED_TWICE;
break;
default:
// clicked too many times, no action taken
break;
}
}
});
private enum State { INITIAL, CLICKED_ONCE, CLICKED_TWICE }
Answered By - Izabela Orlowska
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.