Android入門第一篇

來源:互聯網
上載者:User

最近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的代碼:

view plaincopy to clipboardprint?
<?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> 
<?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代碼:

 


view plaincopy to clipboardprint?
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");  
            }  
        }  
    }  
}  
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);//控制項與代碼綁定
      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.