android 入门demo 解析xml

package com.isoftstone.cry;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.isoftstone.cry.R;

public class AndroidActivity extends Activity 
{
	private Button myBtn ;
	private Button xmlBtn ;
	private TextView xmlView ;
	private ImageView imageView ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /***************button**************************/
        //获取按钮组件
        myBtn = (Button)findViewById(R.id.button1);
        //获取activity资源 
        Resources resources = this.getResources();
        int bwidth = (int)resources.getDimension(R.dimen.btnWidth);
        int bheight = (int)resources.getDimension(R.dimen.btnHeight);
        System.out.println("bwidth = "+bwidth);
        System.out.println("bheight = "+bheight);
        myBtn.setEnabled(false);
        myBtn.setWidth(bwidth);
        myBtn.setHeight(bheight);
        //设置背景颜色
        this.getWindow().setBackgroundDrawableResource(R.color.blue_bg);
        
        /********************xml*********************/
        //设置button实例、textview实例
        xmlBtn = (Button)this.findViewById(R.id.button2);
        xmlView = (TextView)this.findViewById(R.id.textView1);
        //设计button监听
        xmlBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) 
			{
				int count = 0;
				//解析XML文件
				StringBuilder sb = new StringBuilder();
				XmlResourceParser xrp = getResources().getXml(R.xml.test);
				//如果没有到文件尾,继续循环
				try {
					while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT)
					{
						if(xrp.getEventType() == XmlResourceParser.START_TAG)
						{
							String name = xrp.getName();
							if(name.equals("custom"))
							{
								//计数器
								count ++;
								sb.append("第"+count+"客户信息");
								sb.append(xrp.getAttributeName(0)+"\n");
								sb.append(xrp.getAttributeName(1)+"\n");
								sb.append(xrp.getAttributeName(2)+"\n");
							}
						}else if(xrp.getEventType() == XmlResourceParser.END_TAG){
							
						}else if(xrp.getEventType() == XmlResourceParser.TEXT){
							
						}
						//下一个标签
						xrp.next();
					}
					xmlView.setText(sb.toString());
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
        
        /********************drawables*********************/
        imageView = (ImageView)this.findViewById(R.id.imageView2);
        Drawable drawable = this.getResources().getDrawable(R.drawable.psu);
        imageView.setImageDrawable(drawable);
        
        /********************layout*********************/
        
        /********************menu*********************/
        
    }
}

<?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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textColor="@color/red_text" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/getXmlBtn" />
	<ImageView
          android:id="@+id/imageView1"
          android:layout_width="match_parent"
 			android:layout_height="100dp"
			android:src="@drawable/fc" />

      <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>

      <TableLayout
          android:id="@+id/tableLayout1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" >

          <TableRow
              android:id="@+id/tableRow1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >
	        <TextView
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:text="@string/hello"
	        android:textColor="@color/red_text" />
            
	        <TextView
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:text="@string/hello"
	        android:textColor="@color/red_text" />
	        
          </TableRow>

          <TableRow
              android:id="@+id/tableRow2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >
	        
              <Button
		        android:id="@+id/button1"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="@string/btn" />
              <Button
		        android:id="@+id/button1"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="@string/btn" />
              
          </TableRow>

      </TableLayout>
      
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.08"
        android:text="" />
	
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <custom name="tom" age="20" email="[email protected]" />
    <custom name="kite" age="18" email="[email protected]" />
</resources>

你可能感兴趣的:(android demo)