Android入门第一篇layout,onclicklisenter

本文来自http://blog.csdn.net/hellogv/

     

     最近Android挺火的,可惜刚毕业,温饱才刚刚解决,还没能力买台Android手机,所以目前的开发只能用模拟器来做。。。就目前 Android SDK 1.5 + Eclipse + ADT的开发方式来说,跟J2ME最大的区别在于UI的不同,当然Android比J2ME多出很多东西,多出的是J2ME无法作对比的。。。。刚开始做 Android开发,很多人都是先写个简单的界面,再加点控制代码,本文就是这样。

      本文所讲到的是LinearLayout + Button + EditText + AlertDialog的简单使用。


图


Activity以 LinearLayout排列,共用到两个 LinearLayout,第一个是用于全窗体,第二个用于存放两个Button,第二个 LinearLayout放在EditText控件下面,以下给出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" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>  
  8. <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">  
  9. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>  
  10. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>  
  11. </LinearLayout>  
  12. </LinearLayout>  


main.xml用于 Activity的UI设计,目前设计起来的速度,比 J2ME上的LWUIT略快(两者类似,Android提供了GUI设计工具),比WM上的.NET CF略慢(.NETCF 是RAD)。

接下来给出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);//控件与代码绑定  
  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)  
  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)  
  40.             {  
  41.                 edtInput.setText("HelloAndroid");  
  42.             }  
  43.         }  
  44.     }  
  45. }   

刚开始Android的开发,界面设计是J2ME程序员的瓶颈之处,不过以后Android的开发工具会越来越智能化,期待 Netbeans 推出更好的 ADT出来(Netbeans目前已经有Android插件)。

原文地址:https://www.cnblogs.com/hnrainll/p/2248494.html