先在res/drawable-hdpi下准备两张内容一样但大小不同的图片,小的图片名为small,大的图片名为big。
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="/blog_article/@drawable/small/index.html" />
</LinearLayout>
在主Activity(MyIntentCaseDemo.java)中:
package com.li.intentcaseproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MyIntentCaseDemo extends Activity {
private ImageButton mybut = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mybut = (ImageButton) super.findViewById(R.id.mybut);
this.mybut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
MyIntentCaseDemo.this.startActivity(Intent.createChooser(intent,
"请选择图片浏览工具"));
//MyIntentCaseDemo.this.startActivity(intent); 可以选择默认打开
}
}
}
在ImageViewActivity.java中:
package com.li.intentcaseproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
public class ImageViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setTitle("查看图片") ;
ImageView img = new ImageView(this) ;
img.setImageResource(R.drawable.big) ;
super.setContentView(img) ;
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.intentcaseproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".MyIntentCaseDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ImageViewActivity">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="image/jpeg" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:background="#000000">
<EditText
android:id="@+id/tel"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
<Button
android:id="@+id/mybut"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="拨打电话"/>
</LinearLayout>
在MyIntentCaseDemo.java中:
package com.li.intentcaseproject;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
public class MyIntentCaseDemo extends Activity {
private Button mybut = null;
private EditText tel = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mybut = (Button)super.findViewById(R.id.mybut);
this.tel = (EditText)super.findViewById(R.id.tel);
this.mybut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
String telStr = MyIntentCaseDemo.this
.tel.getText().toString(); //取得输入信息
Uri uri = Uri.parse("tel:" + telStr); //设置要操作的路径
Intent it = new Intent();
it.setAction(Intent.ACTION_CALL); //设置要操作的Action
it.setData(uri); //要设置的数据
MyIntentCaseDemo.this.startActivity(it); //执行跳转
}
}
}
修改AndroidManifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.intentcaseproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyIntentCaseDemo"
android:label="@string/title_activity_my_itent_case_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
使用文件保存数据固然很方便,都是如果现在数据较多的话,管理起来就不方便,所以使用文件保存时,
往往会采用XML文件形式进行数据的保存,那么就要对XML文件进行解析,而DOM解析就是最常用
的一种方式。
在AndroidManifest.xml中配置权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.dom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyDOMDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:gravity="center_horizontal"
android:layout_margin="8dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名:" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="李叶文" />
</TableRow>
<TableRow
android:gravity="center_horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮箱:" />
<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="helloliyewen@163.com" />
</TableRow>
<TableRow
android:layout_marginLeft="30dp">
<Button
android:id="@+id/but"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="保存" />
</TableRow>
</TableLayout>
在MyDOMDemo.java程序中
package com.li.dom;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MyDOMDemo extends Activity {
private EditText name = null ;
private EditText email = null ;
private Button but = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.name = (EditText) super.findViewById(R.id.name) ;
this.email = (EditText) super.findViewById(R.id.email) ;
this.but = (Button) super.findViewById(R.id.but) ;
this.but.setOnClickListener(new OnClickListenerImpl()) ;
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 不存在不操作
return; // 返回到程序的被调用处
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "liyewen" + File.separator
+ "test.xml"); // 要输出文件的路径
if (!file.getParentFile().exists()) { // 父路径不存在
file.getParentFile().mkdirs() ; // 创建父文件夹
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;
DocumentBuilder builder = null ;
try {
builder = factory.newDocumentBuilder() ;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null ;
doc = builder.newDocument() ; // 创建一个新的文档
Element addresslist = doc.createElement_x("addresslist") ;
Element linkman = doc.createElement_x("linkman") ;
Element name = doc.createElement_x("name") ;
Element email = doc.createElement_x("email") ;
name.appendChild(doc.createTextNode(MyDOMDemo.this.name.getText()
.toString()));
email.appendChild(doc.createTextNode(MyDOMDemo.this.email.getText()
.toString()));
linkman.appendChild(name) ;
linkman.appendChild(email) ;
addresslist.appendChild(linkman) ;
doc.appendChild(addresslist) ;
TransformerFactory tf = TransformerFactory.newInstance() ;
Transformer t = null ;
try {
t = tf.newTransformer() ;
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
t.setOutputProperty(OutputKeys.ENCODING, "GBK") ;
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file) ;
try {
t.transform(source, result) ;
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
}