一、Click事件与Touch事件的区别:
Click事件:对于Button,既会响应按键,也会响应触摸屏事件;
Touch事件:对于Button,只响应触摸屏事件;
因此通常对于Button控件,最好用Click事件处理。
二、Click事件处理模板
import android.view.View.OnClickListener;
public class xxxx Activityextends Activity
{
private OnClickListener otl_conn = (OnClickListener) new ClickListenerConn();
private Button bv = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxx);
bv = (Button) findViewById(R.id.xxxxbutton);
bv.setOnClickListener(otl_conn);
}
class ClickListenerConn implements OnClickListener
{
public void onClick(View v) {
xxxxxx......
}
}
}
layout/xxxx.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#FFF">
<Button android:id="@+id/connbutton"
android:text="connect"
android:textColor="#000"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
三、Touch事件处理模板
import android.view.View.OnTouchListener;
public class xxxx Activityextends Activity
{
private OnTouchListener otl_conn = (OnTouchListener) new TouchListenerConn();
private Button bv = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxx);
bv = (Button) findViewById(R.id.xxxxbutton);
bv.setOnTouchListener(otl_conn);
}
class TouchListenerConn implements OnTouchListener
{
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
xxxx....;
break;
}
return true;
}
}
}
layout/xxxx.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#FFF">
<Button android:id="@+id/connbutton"
android:text="connect"
android:textColor="#000"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
四、Click事件处理另一精简模板
import android.view.View.OnClickListener;
public class xxxx Activityextends Activity implements View.OnClickListener
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxx);
findViewById(R.id.connbutton).setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.connbutton:
xxxxxx......
break;
}
}
}
转载请注明出处:http://blog.csdn.net/ccwwff/archive/2010/08/18/5821419.aspx