Skip to content

Instantly share code, notes, and snippets.

@SojiroNishimura
Created April 2, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SojiroNishimura/a133cbf88c456efe9a662f279f31f456 to your computer and use it in GitHub Desktop.
Save SojiroNishimura/a133cbf88c456efe9a662f279f31f456 to your computer and use it in GitHub Desktop.
AndroidButtonEventSample
public class MainActivity extends AppCompatActivity {
private final static String TAG = MainActivity.class.getSimpleName();
@InjectView(R.id.button_touch)
Button mTouchButton;
@InjectView(R.id.button_reset)
Button mResetButton;
@InjectView(R.id.tv_x_value)
TextView mXvalue;
@InjectView(R.id.tv_y_value)
TextView mYvalue;
@InjectView(R.id.tv_event_value_touch)
TextView mTouchValue;
@InjectView(R.id.tv_event_value_click)
TextView mClickValue;
@InjectView(R.id.tv_event_value_longclick)
TextView mLongclickValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Injectionを実行すると各メンバに値がセットされる
ButterKnife.inject(this);
}
@OnTouch(R.id.button_touch)
boolean onTouch(View v, MotionEvent event) {
mXvalue.setText(Float.toString(event.getX()));
mYvalue.setText(Float.toString(event.getY()));
mTouchValue.setText(MotionEvent.actionToString(event.getAction()));
// ACTION_CANCEL時のみToast表示
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
Toast.makeText(getApplicationContext(),
MotionEvent.actionToString(event.getAction()), Toast.LENGTH_SHORT).show();
}
Log.d(TAG, "Button touched: " + MotionEvent.actionToString(event.getAction()));
return false;
}
@OnClick({
R.id.button_touch,
R.id.button_reset
})
void onClick(Button b) {
switch (b.getId()) {
case R.id.button_touch:
mClickValue.setText("Button Clicked");
break;
case R.id.button_reset:
clearValues();
break;
default:
break;
}
Log.d(TAG, "Button clicked");
}
@OnLongClick(R.id.button_touch)
boolean onLongClick(Button b) {
mLongclickValue.setText("Button Long Pressed");
Log.d(TAG, "Button Long pressed");
return false;
}
private void clearValues() {
mXvalue.setText("");
mYvalue.setText("");
mTouchValue.setText("");
mClickValue.setText("");
mLongclickValue.setText("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment