編輯:Android開發實例
這個簡單的例子是EditText中默認有個字符串text,單擊Show按鈕,彈出AlertDialog顯示EditText中的內容,單擊Clear按鈕,清除EditText中的內容!!
Activity用到兩個LinearLayout,兩個Button,一個TextView,一個EditText!
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text"
android:id="@+id/text"
/>
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnShow"
android:id="@+id/btnShow"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnClear"
android:id="@+id/btnClear"
/>
</LinearLayout>
</LinearLayout>
java代碼
1 import android.app.Activity;
2 import android.app.AlertDialog;
3 import android.os.Bundle;
4 import android.view.View;
5 import android.widget.Button;
6 import android.widget.EditText;
7
8 public class practice extends Activity {
9 Button btnShow;
10 Button btnClear;
11 EditText text;
12 /** Called when the activity is first created. */
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17
18 btnShow = (Button)findViewById(R.id.btnShow);
19 btnClear = (Button)findViewById(R.id.btnClear);
20 text = (EditText)findViewById(R.id.text);
21
22 btnShow.setOnClickListener(new View.OnClickListener() {
23
24 @Override
25 public void onClick(View v) {
26 // TODO Auto-generated method stub
27 new AlertDialog.Builder(practice.this)
28 .setTitle("Infomation")
29 .setIcon(android.R.drawable.ic_dialog_dialer)
30 .setMessage(text.getText())
31 .show();
32 }
33 });
34
35 btnClear.setOnClickListener(new View.OnClickListener() {
36
37 @Override
38 public void onClick(View v) {
39 // TODO Auto-generated method stub
40 text.setText("");
41 }
42 });
43 }
44 }
轉自:http://www.cnblogs.com/jk1001/archive/2010/08/12/1798105.html
Android開發之SQLite的使用方法
前言 SQLite是一種輕量級的小型數據庫,雖然比較小,但是功能相對比較完善,一些常見的數據庫基本功能也具有,在現在的嵌入式系統中使用該數據庫的比較多,因為
android之自定義Toast使用方法
Android系統默認的Toast十分簡潔,使用也非常的簡單。但是有時我們的程序使用默認的Toast時會和程序的整體風格不搭配,這個時候我們就需要自定義Toast
Android加載Spinner
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
Android開發技巧之在a標簽或TextView控件中單擊鏈接彈出Activity(自定義動作)
在5.2.1節和5.2.2節介紹了<a>標簽以及TextView自動識別的特殊文本(網址、電話號、Email等),這些都可以通過單擊來觸發不同的動作。