1. 程式人生 > >Android入門第一篇

Android入門第一篇

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

本文來自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的程式碼:

[xhtml] view plain copy print ?
  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>  
<?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"    ><EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText><LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button></LinearLayout></LinearLayout>


main.xml用於Activity的UI設計,目前設計起來的速度,比 J2ME上的LWUIT略快(兩者類似,Android提供了GUI設計工具),比WM上的.NET CF略慢(.NETCF 是RAD)。

 

接下來給出JAVA程式碼:


[java] view plain copy print ?
  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. }   
package com.studio.android;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class HelloAndroid extends Activity {    /** Called when the activity is first created. */ Button btnShow; Button btnClear; EditText edtInput;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                btnShow=(Button)findViewById(R.id.btnShow);//控制元件與程式碼繫結        btnClear=(Button)findViewById(R.id.btnClear);//控制元件與程式碼繫結        edtInput=(EditText)findViewById(R.id.edtInput);//控制元件與程式碼繫結        btnShow.setOnClickListener(new ClickListener());//使用點選事件        btnClear.setOnClickListener(new ClickListener());//使用點選事件    }            class  ClickListener implements OnClickListener    {     public void onClick(View v)     {      if(v==btnShow)      {       new AlertDialog.Builder(HelloAndroid.this)       .setIcon(android.R.drawable.ic_dialog_alert)       .setTitle("Information")       .setMessage(edtInput.getText())       .show();        }      else if(v==btnClear)      {       edtInput.setText("HelloAndroid");      }     }    }} 

 

剛開始Android的開發,介面設計是J2ME程式設計師的瓶頸之處,不過以後Android的開發工具會越來越智慧化,期待 Netbeans 推出更好的 ADT出來(Netbeans目前已經有Android外掛)。

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述