【Android笔记】(2)ButtonClickPic
学院居然开了Android的选修课了。不错。恩,星期三下午和星期四上午,那啥。。星期四早上信号和电磁就不去上课浪费时间了,囧。。自己恶补就好。好了,开始.
1.建立工程
Project name :ButtonClickPic
Build Target :2.1
Application name:ButtonClickPic
Package name :com.kero.Android.ButtonClickPic
Creat Activity:ActivityButton
Min SDK:7
2.Clean
为了避免总是显示错误,Project--Build Auto关掉
每次在编译的时候,点Project--Clean...把工程刷新一下,让R生成好。
3首先,onCreate里面显示个按钮。
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> /*在main.xml添加一下代码,直接把Button拖进去也行,挺方便 **然后发现,R里面多了一个button1 **public static final class id { ** public static final int button1=0x7f050000; ** } **就好像MFC里面的ID一样吧 */
4.现在运行一下,看看效果,恩,按钮出来了,然后,实现通过按钮点击,显示图片;显示图片需要另外一个Actvity
然后要给Button弄一个监听
先建一个新的Activity,在包上点右键--new---class--ActivityPic
主要功能就是显示other.xml里面的信息
package com.kero.ButtonClickPic; import android.app.Activity; import android.os.Bundle; public class PicActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); } }
再新建一个other.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:src="@drawable/icon" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="fill_parent" /> </LinearLayout>
接下来是关键了
package com.kero.ButtonClickPic; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ButtonActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonPic = (Button)findViewById(R.id.button1); buttonPic.setOnClickListener(new MyLisentener()); } class MyLisentener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(ButtonActivity.this, PicActivity.class); startActivity(intent); } } }
Button buttonPic = (Button)findViewById(R.id.button1);
buttonPic.setOnClickListener(new MyLisentener());
第一句是,寻找buttonPic的ID,我个人理解为,可以在java里面操控这个按钮了
第二局就是一个监听器,有点像MFC的事件响应吧!
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ButtonActivity.this, PicActivity.class);
startActivity(intent);
}
Listener里面有个函数,就是对onClick这一事件的动作。
Intent 就是个意图,好像打包一样把Activity集合在一起,然后把数据展现出来。
只是初学,所以还不是很清楚原理,总之Intent将需要展示的数据经过 startActivity(intent); 就展示出来了。
很简单。。。- -嘿嘿,看下效果。
Android不是很难,就是东西比较多,慢慢来吧!总有一天会成功。加油!
更多相关文章
- ActionBar的使用
- 显示界面的时候直接获取到EditText焦点弹出键盘
- Android之旋转button视图
- 如何去除android上a标签产生的边框
- android实现打电话和发短信的功能(10学员张晓丛)
- Android从网络中获得一张图片并显示在屏幕上的实例详解
- Android加载长图滑动显示
- Android(安卓)Recovery的汉化 显示中文
- Android(安卓)干货App开发总结