当前位置:  编程技术>移动开发
本页文章导读:
    ▪Intent创设选择器        Intent创建选择器     先在res/drawable-hdpi下准备两张内容一样但大小不同的图片,小的图片名为small,大的图片名为big。     在main.xml中:   <?xml version="1.0" encoding="utf-8"?> <LinearLayout   x.........
    ▪ Intent拨通电话        Intent拨打电话     在main.xml中:   <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="horizonta.........
    ▪ 资料存储 DOM操作       文件存储 DOM操作     使用文件保存数据固然很方便,都是如果现在数据较多的话,管理起来就不方便,所以使用文件保存时, 往往会采用XML文件形式进行数据的保存,那么就要对XML文件进.........

[1]Intent创设选择器
    来源: 互联网  发布时间: 2014-02-18
Intent创建选择器



 

 

先在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>

 

 


    
[2] Intent拨通电话
    来源: 互联网  发布时间: 2014-02-18
Intent拨打电话

 

 

在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>

 


    
[3] 资料存储 DOM操作
    来源: 互联网  发布时间: 2014-02-18
文件存储 DOM操作



 

 

使用文件保存数据固然很方便,都是如果现在数据较多的话,管理起来就不方便,所以使用文件保存时,

往往会采用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();

       }

     }

    

  }

}

 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3