Java:
package com.example.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,listener2Activity.class);
startActivity(intent);
}
});
//焦点事件
button2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
System.out.println("焦点按钮被点击..........");
}
});
button1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
System.out.println("长按.....");
Toast.makeText(MainActivity.this,"按钮被长安了....0.0",Toast.LENGTH_SHORT);
return true;
}
});
//元事件:action_down,action_move,action_up
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
viewGroup.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int actionType = motionEvent.getAction();
if (actionType==MotionEvent.ACTION_DOWN){
System.out.println("touch 按下");
}else if (actionType==MotionEvent.ACTION_MOVE){
System.out.println("touch 移动");
button2.setX(motionEvent.getX());
button2.setY(motionEvent.getY());
}else if (actionType==MotionEvent.ACTION_UP){
System.out.println("touch 松开");
}
return true;
}
});
//焦点事件
button2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
System.out.println("焦点按钮被点击..........");
}
});
button2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//motionEvent:对象记录了事件发生现场情况
int actionType = motionEvent.getAction();
if (actionType==MotionEvent.ACTION_DOWN){
System.out.println("touch 按下");
}else if (actionType==MotionEvent.ACTION_MOVE){
System.out.println("touch 移动");
button2.setX(motionEvent.getX());
button2.setY(motionEvent.getY());
}else if (actionType==MotionEvent.ACTION_UP){
System.out.println("touch 松开");
}
return true;
}
});
//动态设置
button1.setOnClickListener(myListener);
button2.setOnClickListener(myListener);
button3.setOnClickListener(myListener);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("发生点击事件");
Toast.makeText(MainActivity.this,"被点击了",Toast.LENGTH_SHORT).show();
}
});
//配置方式设置
android:onClick="button2"
public void button3(View view){
Toast.makeText(MainActivity.this,"点击了按钮",Toast.LENGTH_SHORT).show();
}
// 可复用式
}
private View.OnClickListener myListener= new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
Toast.makeText(MainActivity.this,"button1被点击了",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this,"button2被点击了",Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(MainActivity.this,"button3被点击了",Toast.LENGTH_SHORT).show();
break;
}
}
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.helloworld.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</TableLayout>
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="button1"
/>
<Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_marginTop="38dp"
android:id="@+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="button3"
/>
<Button
android:text="Button2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:onClick="button2"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="77dp"
android:layout_marginStart="77dp" />
</RelativeLayout>