onclick onlongclick ontouch

public class ListenerTestActivity extends Activity
{
private Button mButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mButton = (Button) findViewById(R.id.button);

//设置监听
mButton.setOnTouchListener(touchListener); //touch
mButton.setOnClickListener(clickListener); //click
mButton.setOnLongClickListener(longClickListener); //long click
}


public OnTouchListener touchListener = new OnTouchListener()
{
/*
* 不同的return值有不同的情况
*/
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)

Log.i("log", "action_down");
return true;
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
Log.i("log", "action_up");
return true;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
Log.i("log", "action_move");
return true;
}
return false;
}
};

public OnClickListener clickListener = new OnClickListener()
{

public void onClick(View v)
{
Log.i("log", "click");
}
};


public OnLongClickListener longClickListener = new OnLongClickListener()
{

public boolean onLongClick(View v)
{
Log.i("log", "long click");
return false;
}
};
}

你可能感兴趣的:(onclick onlongclick ontouch)