标签:
1 package com.example.a07_button; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.util.Log; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.Toast; 12 13 public class MainActivity extends Activity implements OnClickListener { 14 private Button btn, btn1, btn2, btn3, btn4, btn5; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 findbyid();// 实现这个方法,调用写完,在去写实现 ,Ctrl+1就可以实现 21 btn.setOnClickListener(new OnClickListener() { 22 23 @Override 24 public void onClick(View v) { 25 // Toast叫做文本提示框 26 Toast.makeText(getApplicationContext(), "匿名内部类的形式", 1).show(); 27 /** 28 * 参数1 :上下文对象 参数2: 打印信息 参数3: 延迟时间 29 */ 30 } 31 }); 32 btn1.setOnClickListener(new onclickli());// 内部类的实现 33 outclick o = new outclick(getApplicationContext());// 创建他的对象传递getApplicationContext 34 btn2.setOnClickListener(o); 35 36 btn3.setOnClickListener(this);// this 代表的是当前类的对象 MainActivity 37 btn4.setOnClickListener(this); 38 } 39 40 private void findbyid() { 41 btn = (Button) findViewById(R.id.button1);// 匿名内部类的形式 42 btn1 = (Button) findViewById(R.id.button2);// 内部类的实现 43 btn2 = (Button) findViewById(R.id.button3);// 外部类的实现 44 btn3 = (Button) findViewById(R.id.button4);// Activity的接口实现 45 btn4 = (Button) findViewById(R.id.button5); 46 btn5 = (Button) findViewById(R.id.button6);// 在XMl使用android:onClick="MyLinsten" 47 48 } 49 50 class onclickli implements OnClickListener {// 内部类的实现 51 52 @Override 53 public void onClick(View v) { 54 Toast.makeText(getApplicationContext(), "内部类的实现", 1).show(); 55 56 } 57 58 } 59 60 public void MyLinsten(View v) {// 这是第五种方法 61 Intent i = new Intent(getApplicationContext(), SecondImageButton.class); 62 startActivity(i); 63 64 } 65 66 @Override 67 public void onClick(View v) { 68 switch (v.getId()) { 69 case R.id.button4: 70 Toast.makeText(getApplicationContext(), "这是按钮4", 1).show(); 71 72 break; 73 case R.id.button5: // Imageview的跳转 74 Toast.makeText(getApplicationContext(), "这是按钮5", 1).show(); 75 Intent i = new Intent(getApplicationContext(), thirdImageView.class); 76 startActivity(i); 77 78 break; 79 default: 80 break; 81 } 82 83 } 84 85 } 86
=====================================================================================
outclick.java:
1 package com.example.a07_button; 2 3 import com.example.a07_button.MainActivity.onclickli; 4 5 import android.content.Context; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Toast; 9 10 public class outclick implements OnClickListener { 11 /** 12 * Context(上下文对象):他只有在activity里面有这个对象 13 */ 14 private Context context; 15 16 public outclick(Context context) { 17 //使用构造器进行传递context 赋值给我们的属性并声明全局,以供给我们的onclick方法使用 18 this.context = context; 19 } 20 21 @Override 22 public void onClick(View v) { 23 Toast.makeText(context, "当前为外部类", 1).show(); 24 } 25 26 }
===================================================================================
1 package com.example.a07_button; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.ImageButton; 8 9 public class SecondImageButton extends Activity { 10 private ImageButton ib2; 11 private boolean flag = true; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 // TODO Auto-generated method stub 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_second); 18 /** 19 * 当我们设置为src时 android:src="@drawable/nvjing" 改变图片用的是 20 * 21 * ib2.setImageResource(R.drawable.hanbing); 当我们设置为background 22 */ 23 ib2 = (ImageButton) findViewById(R.id.ib); 24 ib2.setOnClickListener(new OnClickListener() { 25 26 @Override 27 public void onClick(View v) { 28 if (flag) { 29 ib2.setBackgroundResource(R.drawable.nvjing); 30 flag = false; 31 // 对应android:background="@drawable/hanbing" 32 } else { 33 ib2.setBackgroundResource(R.drawable.hanbing);// 对应android:background="@drawable/hanbing" 34 flag = true; 35 } 36 37 // ib2.setImageResource(R.drawable.hanbing);//这是对应src的属性 38 39 } 40 }); 41 } 42 }
1 package com.example.a07_button; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.ImageView; 9 10 public class thirdImageView extends Activity implements OnClickListener { 11 private Button pre, next; 12 private ImageView iv; 13 private int iamges[]; 14 private int current; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_third); 21 findbyid(); 22 Init(); 23 } 24 25 private void Init() { 26 iamges = new int[] { R.drawable.hanbing, R.drawable.kate, 27 R.drawable.nvjing }; 28 // iv.setImageDrawable(getResources().getDrawable(R.drawable.kate));//寻找型 29 iv.setBackgroundResource(R.drawable.kate);// 对应的是第二章图片 30 current = 1; 31 32 } 33 34 private void findbyid() { 35 pre = (Button) findViewById(R.id.pre); 36 next = (Button) findViewById(R.id.next); 37 iv = (ImageView) findViewById(R.id.iv); 38 pre.setOnClickListener(this); 39 next.setOnClickListener(this); 40 41 } 42 43 @Override 44 public void onClick(View v) { 45 switch (v.getId()) { 46 case R.id.pre:// 上一张 47 current = (current - 1 + iamges.length) % iamges.length; 48 49 iv.setBackgroundResource(iamges[current]); 50 break; 51 case R.id.next:// 下一张 52 current = (current + 1 + iamges.length) % iamges.length; 53 54 iv.setBackgroundResource(iamges[current]); 55 break; 56 default: 57 break; 58 } 59 60 } 61 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.a07_button" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 <uses-sdk 7 android:minSdkVersion="10" 8 android:targetSdkVersion="10" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity 16 android:name="com.example.a07_button.MainActivity" 17 android:label="@string/app_name" > 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 <activity android:name="com.example.a07_button.SecondImageButton" > 25 </activity> 26 <activity android:name="com.example.a07_button.thirdImageView" > 27 </activity> 28 </application> 29 </manifest> 30
标签:
原文地址:http://www.cnblogs.com/Je-Cortex/p/4830969.html