android创建自定义搜索框dialog。本例主要包含2个Activity类和2个Layout xml文件
activity_main1.xml 主界面包含一行文字和一个Button,点击
startdialog这个button就显示自定义的对话框。
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical" >
-
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="点击按钮显示自定义Dialog" />
-
-
<Button
-
android:id="@+id/startdialog"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="Show Custom Dialog" >
-
</Button>
-
-
</LinearLayout>
SearchMain.java
-
package com.example.android;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
public class SearchMain extends Activity
-
{
-
-
private Button startDialogBtn;
-
private static final int MY_CUSTOM_DIALOG = 0;
-
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main1);
-
startDialogBtn = (Button) findViewById(R.id.startdialog);
-
startDialogBtn.setOnClickListener(new OnClickListener() {
-
public void onClick(View v)
-
{
-
startCustomDialog();
-
}
-
});
-
}
-
-
/**
-
* Starts an Activity which in this case is a custom dialog, as specified in
-
* AndroidManifest.xml
-
*/
-
private void startCustomDialog()
-
{
-
Intent intent = new Intent(this, SearchDialog.class);
-
startActivityForResult(intent, MY_CUSTOM_DIALOG);
-
}
-
-
@Override
-
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);
-
Toast.makeText(getApplicationContext(),searchQuery ,Toast.LENGTH_LONG).show();
-
}
-
break;
-
}
-
}
-
}
-
}
activity_searchdialog.xml 搜索对话框布局文件 。包含一个
EditText和2个Button
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:id="@+id/layout_root"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical"
-
android:padding="10dp" >
-
-
<EditText
-
android:id="@+id/search_query"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content" />
-
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:background="@android:drawable/bottom_bar"
-
android:orientation="horizontal"
-
android:paddingBottom="1.0dip"
-
android:paddingLeft="4.0dip"
-
android:paddingRight="4.0dip"
-
android:paddingTop="5.0dip" >
-
-
<Button
-
android:id="@+id/search"
-
android:layout_width="0.0dip"
-
android:layout_height="fill_parent"
-
android:layout_weight="1.0"
-
android:text="Search" />
-
-
<Button
-
android:id="@+id/cancel"
-
android:layout_width="0.0dip"
-
android:layout_height="fill_parent"
-
android:layout_weight="1.0"
-
android:text="Cancel" />
-
</LinearLayout>
-
-
</LinearLayout>
AndroidManifest.xml
部分:
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
-
-
<activity android:name=".SearchMain"
-
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="Search Dialog"
-
android:theme="@android:style/Theme.Dialog"/>
-
</application>
点击主界面中的按钮后,弹出搜索对话框的效果图:
阅读(2247) | 评论(0) | 转发(0) |