Android开发小记
一,下载解压adt-bundle,直接可以用来开发了
二,新建android项目时不勾选创建activity,来看看如何手动创建activity
1,在空项目添加class文件,选择超类为activity
package com.ciaos.demo; public class DemoActivity extends Activity { }
2,双击AndroidManifest.xml,选择Application/Add/Activity,配置Name为刚创建的activity
3,右键layout新建Android XML File,选择layout类型,生成main.xml文件,可以通过R.layout.main引用
4,编辑DemoActivity.java文件如下,设置Activity的Layout样式
package com.ciaos.demo; import com.ciaos.test1.R; import android.app.Activity; import android.os.Bundle; public class DemoActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
5,运行android程序
问题1,如果eclipse编码时提示找不到R.layout.main(自定义layout)
-> 修改DemoActivity.java将import android.R改为import com.ciaos.test1.R
问题2,No Launcher activity found
-> 修改AndroidManifest.xml文件,添加起的那个activity
<activity android:name="com.ciaos.demo.DemoActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
三,通过button控制text的显示
1,设置res/values/strings.xml添加自定义文本
<resources> <string name="app_name">Test1</string> <string name="shige">锄禾日当午\n汗滴禾下土</string> <string name="anniu">按钮显示文本</string> </resources>
2,拖动Button和TextView控件到main.xml文件,配置TextView隐藏,Button的click事件,xml文件如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onShowClicked" android:text="@string/anniu" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="invisible" android:text="@string/shige" /> </LinearLayout>
3,编辑DemoActivity文件,实现onShowClicked函数
package com.ciaos.demo; import com.ciaos.test1.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class DemoActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onShowClicked(View view){ TextView tv = (TextView)this.findViewById(R.id.textView1); tv.setVisibility(View.VISIBLE); } }
四,网络请求(应用加载时获取网络图片更新组件UI)
1,编辑AndroidManifest添加Uses Permission
<uses-permission android:name="android.permission.INTERNET"/>
2,编辑工具类ImageHandler,实现下载图片功能
package com.ciaos.demo; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class ImageHandler{ private String url = "http://www.baidu.com/img/baidu_sylogo1.gif"; public Bitmap getImage() { try { HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(input); input.close(); return bitmap; } catch (IOException ioe) { return null; } } }
3,修改Activity文件,通过子线程调用图片下载服务,使用Handler更新主线程创建的控件
package com.ciaos.demo; import com.ciaos.test1.R; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.widget.ImageView; public class DemoActivity extends Activity { Handler handler; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建主窗口的引用 handler = new Handler(); //显示等待提示框 final ProgressDialog dialog = ProgressDialog.show( this, "Loading", "Loading the image"); final ImageHandler nh = new ImageHandler(); Thread th = new Thread() { public void run() { //获取网页图片 final Bitmap bm = nh.getImage(); handler.post(new Runnable(){ @Override public void run() { //更新组件 resetDisplay(bm); //取消等待提示框 dialog.dismiss(); }}); } }; th.start(); } private void resetDisplay(Bitmap image){ //更新组件 ImageView iv = (ImageView)this.findViewById(R.id.imgView1); iv.setImageBitmap(image); } }
运行程序后多次横屏竖屏后,程序会出现java.lang.IllegalArgumentException: View not attached to window manager错误,估计是我调用ProgressDialog创建及dismiss方法有误。
多个Activity之间切换
Intent it = new Intent(this,Demo2Activity.class);
startActivity(it);
附操作技巧:如果一个项目是生成jar包供别的android项目调用,可以右键项目->Property->Android(勾选IsLibrary),则每次clean后都会自动生成jar包。