android基础第一篇 UI设计

        最近学习android,对于ui设计有个基础的了解。Android的界面设计可以在java代码中实现 也可以在xml文件中实现,xml文件的解析有两种Dom和Sax,设计方面一般用Sax解析xml文件。对于xml的解析,这里不做解释。现在让我们来初步认识认识Android开发的基础UI设计。

        假设读者安装好了ADT和Eclipse。首先打开Eclipse,新建一个Android工程,建自己的包名,文件名,和工程名。打开工程目录,打开res/layout目录下的main.xml文件,设计如下

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <EditText android:text="EditText01" android:layout_height="wrap_content" 
  8. android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>  
  9. <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content"
  10.  android:layout_width="fill_parent" android:gravity="center">  
  11. <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
  12.  android:text="Show" android:id="@+id/btnShow"></Button>  
  13. <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
  14.  android:text="Clear" android:id="@+id/btnClear"></Button>  
  15. </LinearLayout>  
  16. </LinearLayout>  

效果图:

    上面xml文件LinearLayout 线性布局 ,android的布局还有帧布局,相对布局,表格布局,绝对布局分别是FrameLayout、RelativeLayout 、TableLayout 、AbsoluteLayout。LinearLayout有个orientation属性,是水平布局(vertical)还是垂直布局(Hrizontal )

接下来些java代码,在src\com\example\activitys目录下打开MainActivity.java,添加如下代码:

  1. package com.studio.android;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. public class HelloAndroid extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     Button btnShow;  
  12.     Button btnClear;  
  13.     EditText edtInput;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         btnShow=(Button)findViewById(R.id.btnShow);//控件的id获得控件   
  20.         btnClear=(Button)findViewById(R.id.btnClear);//同上
  21.         edtInput=(EditText)findViewById(R.id.edtInput);// 同上
  22.         btnShow.setOnClickListener(new ClickListener());//注册点击事件 
  23.         btnClear.setOnClickListener(new ClickListener());//同上  
  24.     }  
  25.       
  26.       
  27.     class  ClickListener implements OnClickListener  
  28.     {  
  29.         public void onClick(View v)  
  30.         {  
  31.             if(v==btnShow)   //对按钮show的点击事件的响应
  32.             {  
  33.                 new AlertDialog.Builder(HelloAndroid.this)  
  34.                 .setIcon(android.R.drawable.ic_dialog_alert)  
  35.                 .setTitle("Information")  
  36.                 .setMessage(edtInput.getText())  
  37.                 .show();          
  38.             }  
  39.             else if(v==btnClear)    //对按钮clear的点击事件的响应
  40.             {  
  41.                 edtInput.setText("HelloAndroid");  
  42.             }  
  43.         }  
  44.     }  
  45. }   
   发布工程,我们第一个Android界面就算做好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值