Android入门第一篇

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


图


Activity以 LinearLayout排列,共用到两个 LinearLayout,第一个是用于全窗体,第二个用于存放两个Button,第二个 LinearLayout放在EditText控件下面,以下给出main.xml的代码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <EditTextandroid:text="EditText01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/edtInput"></EditText>
  8. <LinearLayoutandroid:id="@+id/LinearLayout01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center">
  9. <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Show"android:id="@+id/btnShow"></Button>
  10. <Buttonandroid: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代码:


  1. packagecom.studio.android;
  2. importandroid.app.Activity;
  3. importandroid.app.AlertDialog;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.widget.Button;
  8. importandroid.widget.EditText;
  9. publicclassHelloAndroidextendsActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. ButtonbtnShow;
  12. ButtonbtnClear;
  13. EditTextedtInput;
  14. @Override
  15. publicvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. btnShow=(Button)findViewById(R.id.btnShow);//控件与代码绑定
  19. btnClear=(Button)findViewById(R.id.btnClear);//控件与代码绑定
  20. edtInput=(EditText)findViewById(R.id.edtInput);//控件与代码绑定
  21. btnShow.setOnClickListener(newClickListener());//使用点击事件
  22. btnClear.setOnClickListener(newClickListener());//使用点击事件
  23. }
  24. classClickListenerimplementsOnClickListener
  25. {
  26. publicvoidonClick(Viewv)
  27. {
  28. if(v==btnShow)
  29. {
  30. newAlertDialog.Builder(HelloAndroid.this)
  31. .setIcon(android.R.drawable.ic_dialog_alert)
  32. .setTitle("Information")
  33. .setMessage(edtInput.getText())
  34. .show();
  35. }
  36. elseif(v==btnClear)
  37. {
  38. edtInput.setText("HelloAndroid");
  39. }
  40. }
  41. }
  42. }

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插件)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值