Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android中自定義對話框小結

Android中自定義對話框小結

編輯:高級開發

今天小結下在android中兩個activity之間傳遞數據的知識點,其中還參考用了個模糊的效果。

  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>

  }

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved