編輯:高級開發
1 主程序AndroDialog
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.util.Log;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
public class AndroDialog extends Activity {
/** Called when the activity is first created. */
private Button startDialogBtn;
private static final int MY_CUSTOM_DIALOG = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
startDialogBtn = (Button)findVIEwById(R.id.startdialog);
startDialogBtn.setOnClickListener(new OnClickListener(){
public void onClick(VIEw v) {
startCustomDialog();
}
});
}
private void startCustomDialog() {
Intent intent = new Intent(this,SearchDialog.class);
startActivityForResult(intent, MY_CUSTOM_DIALOG);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (MY_CUSTOM_DIALOG) : {
if (resultCode == Activity.RESULT_OK) {
//這裡用日志去記錄從自定義對話框中返回的結果
Log.d("ANDRO_DIALOG","Coming back from the search dialog..");
//獲得返回的數據
String searchQuery = data.getStringExtra(SearchDialog.SEARCH_QUERY_RESULT_FROM_DIALOG);
Log.d("ANDRO_DIALOG", "Search query result: " + searchQuery);
}
break;
}
}
對應的mail.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"
/>
< Button android:text="Show Custom Dialog"
android:id="@+id/startdialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
< /Button>
< /LinearLayout>
2 彈出的第二個對話框
SearchDialog.Java
package com.liao;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.VIEw;
import android.vIEw.WindowManager;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SearchDialog extends Activity{
public static final String SEARCH_QUERY_RESULT_FROM_DIALOG = "SEARCH_DIALOG";
private Button searchBtn;
private Button cancelBtn;
private EditText searchEdit;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.dialog);
searchBtn = (Button)findVIEwById(R.id.search);
searchBtn.setOnClickListener(new OnClickListener(){
public void onClick(VIEw v) {
returnSearchQuery();
}
});
cancelBtn = (Button)findVIEwById(R.id.cancel);
cancelBtn.setOnClickListener(new OnClickListener(){
public void onClick(VIEw v) {
cancelDialog();
}
});
接上頁
searchEdit = (EditText)findVIEwById(R.id.search_query);
searchEdit.setText("query");
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
}
private void returnSearchQuery(){
Intent resultIntent = new Intent(this, SearchDialog.class);
resultIntent.putExtra(SEARCH_QUERY_RESULT_FROM_DIALOG, searchEdit.getText().toString());
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
private void cancelDialog(){
finish();
}
}
dialog.XML
< LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orIEntation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
< EditText android:id="@+id/search_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
< LinearLayout android:orIEntation="horizontal"
android:background="@android:drawable/bottom_bar" android:paddingLeft="4.0dip"
android:paddingTop="5.0dip" android:paddingRight="4.0dip"
android:paddingBottom="1.0dip" android:layout_width="fill_parent"
android:layout_height="wrap_content">
< Button android:id="@+id/search" android:layout_width="0.0dip"
android:layout_height="fill_parent" android:text="Search"
android:layout_weight="1.0" />
< Button android:id="@+id/cancel" android:layout_width="0.0dip"
android:layout_height="fill_parent" android:text="Cancel"
android:layout_weight="1.0" />
< /LinearLayout>
< /LinearLayout>
配置文件:
< ?XML version="1.0" encoding="utf-8"?>
接上頁
< manifest XMLns:android="http://schemas.android.com/apk/res/android"
package="com.liao"
android:versionCode="1"
android:versionName="1.0">
< application android:icon="@drawable/icon" android:label="@string/app_name">
< activity android:name=".AndroDialog"
android:label="@string/app_name">
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< /intent-filter>
< /activity>
< activity android:name=".SearchDialog"
android:label="SearchDialog"
android:theme="@android:style/Theme.Dialog"/>
< /application>
< uses-sdk android:minSdkVersion="8" />
< /manifest>
}
}
下面介紹下有關android源代碼的內容和編程技巧,但是首先要讓大家了解下什麼是android操作系統,所謂的android系統:基於Linux平台的開源手機操作系統。
android DDMS將為IDE搭建起與測試終端的鏈接,它們應用各自獨立的端口監聽調試器的信息,android DDMS最大的特性就是可以實時監測到測試終端的連接情況
很多人對android2.0已經十分期待了,鑒於強大的功能性以及快速。android未來的市場將是十分廣闊的。搭載Android2.0源碼的摩托羅拉Droid手機已經上
開發人員可以為特定的運營商創建專屬游戲、日程表和其他手機應用時,android 平台已經得到中國手機產業鏈的廣泛關注和支持,當然這一切都不是android機型所能實現。