这是一个非常简单的Hello world 应用程序,通过这个应用程序,可以看到如何通过创建XML来布局Android应用程序的GUI.

1. res/layout/main.xml

<?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" > <TextView android:id="@+id/showInstruction" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="normal" android:text="@string/instruction" /> <EditText android:id="@+id/textarea1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:clickable="true" android:singleLine="true" /><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button1_text" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button2_text" /> <TextView android:id="@+id/showSavedValue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" /> </LinearLayout>

2. res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TextBox</string> <string name="button1_text">Show Value</string> <string name="instruction">Type some value</string> <string name="button2_text">abc</string> </resources>

3. src/TexBox.java

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class TextBox extends Activity implements View.OnClickListener { private EditText myText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Button myButton = (Button) findViewById(R.id.button1); myText = (EditText) findViewById(R.id.textarea1); myButton.setOnClickListener(this); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.i("Example1", "Clicked Button 2"); } }); } public void onClick(View arg0) { TextView savedText = (TextView) findViewById(R.id.showSavedValue); savedText.setText(myText.getText()); savedText.setTextSize(24); } }

更多相关文章

  1. 《Android(安卓)Security Internals》第一章安卓安全模型翻译
  2. AndroidManifest.xml的Service元素 android:process设置
  3. android创建桌面快捷方式(启动目标非项目的启动页)
  4. android弹出对话框
  5. Android在WebView上构建Web应用程序
  6. Android(安卓)横竖屏操作
  7. android -- 多级目录创建
  8. android4.3应用程序隐藏状态栏和标题栏
  9. Google Android软件架构

随机推荐

  1. Android中添加布局和初始化布局总结
  2. android取得账户(邮件地址)
  3. android IOC框架学习记录
  4. Android常用的工具类
  5. ubuntu10.04上android环境安装 “org.ecl
  6. android Webview基本配置以及android与js
  7. Android(安卓)倒计时(支持小时,分钟)
  8. ANDROID – 使用特定的URL开启应用程序
  9. 获取Android正在运行的任务和服务
  10. Unable to execute dex: Multiple dex fi