Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2537674
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Android平台

2013-04-26 13:33:09

android创建自定义搜索框dialog。本例主要包含2个Activity类和2个Layout xml文件

activity_main1.xml 主界面包含一行文字和一个Button,点击startdialog这个button就显示自定义的对话框。

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="点击按钮显示自定义Dialog" />

  10.     <Button
  11.         android:id="@+id/startdialog"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content"
  14.         android:text="Show Custom Dialog" >
  15.     </Button>

  16. </LinearLayout>

SearchMain.java

点击(此处)折叠或打开

  1. package com.example.android;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.Toast;

  10. public class SearchMain extends Activity
  11. {

  12.     private Button startDialogBtn;
  13.     private static final int MY_CUSTOM_DIALOG = 0;

  14.     /** Called when the activity is first created. */
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState)
  17.     {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main1);
  20.         startDialogBtn = (Button) findViewById(R.id.startdialog);
  21.         startDialogBtn.setOnClickListener(new OnClickListener() {
  22.             public void onClick(View v)
  23.             {
  24.                 startCustomDialog();
  25.             }
  26.         });
  27.     }

  28.     /**
  29.      * Starts an Activity which in this case is a custom dialog, as specified in
  30.      * AndroidManifest.xml
  31.      */
  32.     private void startCustomDialog()
  33.     {
  34.         Intent intent = new Intent(this, SearchDialog.class);
  35.         startActivityForResult(intent, MY_CUSTOM_DIALOG);
  36.     }

  37.     @Override
  38.     public void onActivityResult(int requestCode, int resultCode, Intent data)
  39.     {
  40.         super.onActivityResult(requestCode, resultCode, data);
  41.         switch (requestCode)
  42.         {
  43.         case (MY_CUSTOM_DIALOG):
  44.         {
  45.             if (resultCode == Activity.RESULT_OK)
  46.             {
  47.                 Log.d("ANDRO_DIALOG", "Coming back from the search dialog..");
  48.                 String searchQuery = data.getStringExtra(SearchDialog.SEARCH_QUERY_RESULT_FROM_DIALOG);
  49.                 Log.d("ANDRO_DIALOG", "Search query result: " + searchQuery);
  50.                 Toast.makeText(getApplicationContext(),searchQuery ,Toast.LENGTH_LONG).show();
  51.             }
  52.             break;
  53.         }
  54.         }
  55.     }
  56. }

activity_searchdialog.xml 搜索对话框布局文件 。包含一个EditText和2个Button

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:id="@+id/layout_root"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:orientation="vertical"
  7.     android:padding="10dp" >

  8.     <EditText
  9.         android:id="@+id/search_query"
  10.         android:layout_width="fill_parent"
  11.         android:layout_height="wrap_content" />

  12.     <LinearLayout
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content"
  15.         android:background="@android:drawable/bottom_bar"
  16.         android:orientation="horizontal"
  17.         android:paddingBottom="1.0dip"
  18.         android:paddingLeft="4.0dip"
  19.         android:paddingRight="4.0dip"
  20.         android:paddingTop="5.0dip" >

  21.         <Button
  22.             android:id="@+id/search"
  23.             android:layout_width="0.0dip"
  24.             android:layout_height="fill_parent"
  25.             android:layout_weight="1.0"
  26.             android:text="Search" />

  27.         <Button
  28.             android:id="@+id/cancel"
  29.             android:layout_width="0.0dip"
  30.             android:layout_height="fill_parent"
  31.             android:layout_weight="1.0"
  32.             android:text="Cancel" />
  33.     </LinearLayout>

  34. </LinearLayout>


AndroidManifest.xml

部分:

点击(此处)折叠或打开

  1. <application
  2.         android:allowBackup="true"
  3.         android:icon="@drawable/ic_launcher"
  4.         android:label="@string/app_name"
  5.         android:theme="@style/AppTheme" >
  6.        
  7.         
  8.           <activity android:name=".SearchMain"
  9.                   android:label="@string/app_name">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN" />
  12.                 <category android:name="android.intent.category.LAUNCHER" />
  13.             </intent-filter>
  14.         </activity>
  15.         <activity android:name="SearchDialog"
  16.           android:label="Search Dialog"
  17.           android:theme="@android:style/Theme.Dialog"/>
  18.     </application>

点击主界面中的按钮后,弹出搜索对话框的效果图:


阅读(2205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~