(1)自身類作為事件監聽器 package cn.edu.gdmec.s07150745.work5; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View; ...
(1)自身類作為事件監聽器
package cn.edu.gdmec.s07150745.work5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setText("單擊");
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(this,"已單擊我...",Toast.LENGTH_SHORT).show();
System.out.println("已單擊我...");
}
}
(2)外部類作為事件監聽器:
package cn.edu.gdmec.s07150745.work5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Act2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act2);
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setText("單擊");
btn2.setOnClickListener(new OuterClass("已單擊我..."));
}
}
class OuterClass implements View.OnClickListener{
private String str="已單擊我....";
public OuterClass(String str){
super();
this.str=str;
}
@Override
public void onClick(View v) {
System.out.println(str);
}
}
(3)內部類作為事件監聽器:
package cn.edu.gdmec.s07150745.work5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Act3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act3);
Button btn3 = (Button) findViewById(R.id.btn3);
btn3.setText("單擊");
btn3.setOnClickListener(new OuterClass());
}
class OuterClass implements View.OnClickListener{
@Override
public void onClick(View v) {
Toast.makeText(Act3.this,"已單擊我...",Toast.LENGTH_SHORT).show();
System.out.println("已單擊我...");
}
}
}
(4)匿名類作為事件監聽器:
package cn.edu.gdmec.s07150745.work5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Act4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act4);
Button btn4 = (Button) findViewById(R.id.btn4);
btn4.setText("單擊");
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Act4.this,"已單擊我...",Toast.LENGTH_SHORT).show();
System.out.println("已單擊我...");
}
});
}
}
以上四種方法之後,在xml文件中給每一個事件即activity裡面定義多一個android label即可。不忘在每個activity裡面添加如下藍色的一段代碼哦。
<activity android:name=".MainActivity" android:label="1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>