这是一个非常简单的Hello world 应用程序,通过这个应用程序,可以看到如何通过创建XML来布局Android应用程序的GUI.
1. res/layout/main.xml
<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
<string name="button2_text">abc</string>
</resources>
3. src/TexBox.java
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);
}
}