Android 第一个helloWorld


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*********************/

}
}


layout_main.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"
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值