This article uses a simple example to introduce you to the introductory knowledge of Android programming. This case is an early example program. Readers can compare and study it to fully understand the evolution of Android programs to deepen their understanding of Android program design. The program running platform is Android SDK 1.5 + Eclipse + ADT. The biggest difference between Android and J2ME is the difference in UI. Of course, Android has many more things than J2ME, and there are many more things that cannot be compared with J2ME. When they first start doing Android development, many people first write a simple interface and then add some control code. This is the case in this article.
The example in this article describes the simple use of LinearLayout + Button + EditText + AlertDialog.
Activity is arranged in LinearLayout, sharing two LinearLayout, the first one is used for the full form, the second one is used to store two Buttons, and the second LinearLayout is placed under the EditText control.
The code for main.xml is given below:
<?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 is used for the UI design of Activity. The current design speed is slightly faster than LWUIT on J2ME (the two are similar, Android provides GUI design tools), and slightly slower than .NET CF on WM (.NETCF is RAD ).
Next, the JAVA code is given:
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);//Control and code binding btnClear=(Button)findViewById(R.id.btnClear);//Control and code binding edtInput=(EditText)findViewById(R .id.edtInput);//Control and code binding btnShow.setOnClickListener(new ClickListener());//Use click events btnClear.setOnClickListener(new ClickListener());//Use click events} 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"); } } }}
At the beginning of Android development, interface design was the bottleneck for J2ME programmers. However, in the future, Android development tools will become more and more intelligent. I look forward to Netbeans launching a better ADT (Netbeans currently has Android plug-ins). I hope that the examples described in this article will be helpful to everyone in learning Android.