最近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的代码:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditTextandroid:text="EditText01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/edtInput"></EditText>
- <LinearLayoutandroid:id="@+id/LinearLayout01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center">
- <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Show"android:id="@+id/btnShow"></Button>
- <Buttonandroid: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代码:
- packagecom.studio.android;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- publicclassHelloAndroidextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- ButtonbtnShow;
- ButtonbtnClear;
- EditTextedtInput;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- 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(newClickListener());//使用点击事件
- btnClear.setOnClickListener(newClickListener());//使用点击事件
- }
- classClickListenerimplementsOnClickListener
- {
- publicvoidonClick(Viewv)
- {
- if(v==btnShow)
- {
- newAlertDialog.Builder(HelloAndroid.this)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Information")
- .setMessage(edtInput.getText())
- .show();
- }
- elseif(v==btnClear)
- {
- edtInput.setText("HelloAndroid");
- }
- }
- }
- }