这是一个非常简单的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. LinearLayout布局之weight
  2. Android 布局之LinearLayout
  3. 基本布局之线性布局(LinearLayout)
  4. android4.3应用程序隐藏状态栏和标题栏
  5. android五种布局特点
  6. 在代码中设置RelativeLayout布局中标签的android:layout_toLeftO
  7. 一、 Android完全退出应用程序
  8. Android应用程序绑定服务(bindService)的过程

随机推荐

  1. ADT在线安装连接不上的解决办法
  2. android里在程序中修改listView里的内容
  3. AutoCompleteTextView与MultiAutoComplet
  4. android中webView JS调用Android的方法、
  5. Android获取屏幕的宽和高
  6. Android 中文API (37) —— AbsoluteLayout
  7. android超快模拟器Ggenymotion的安装和配
  8. android复制数据库到SD卡
  9. Android热插拔事件处理流程--Vold
  10. Android installation problem on Ubuntu