我有一个带有列表的主活动和一个接收关键字的方法,可以在数据库中进行搜索。
我希望使用搜索框实现AlertDialog
,当我通过此表单中的动作侦听器单击按钮时,该搜索框应显示在主活动上
OnClickListener searchListener = new OnClickListener() {
public void onClick(View v) {
// ...
}
};
此对话框应该有一个简单的EditText
框和两个按钮(搜索和取消),并且必须通过所选的String
EditText
到表单中的搜索方法:
public void searchWord(String keyword){
// ....search in DB
// ...updateListGUIWithNewValues()
}
此方法是获取新列表值的主要活动,因此活动可以更新GUI列表。
我已经实施了搜索方法和主要活动,但我不知道实现此类对话的正确方法。
答案 0 :(得分:4)
这是我的警报对话框代码,其中包含编辑文本。我在按钮单击侦听器中调用它,因此它不是一个单独的方法。我上周在这个网站的某处发现了它,但我似乎无法找到它,所以我可以引用它。
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Manual Item Search");
alert.setMessage("Input Search Query");
// Set an EditText view to get user input
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String result = input.getText().toString();
//do what you want with your result
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
答案 1 :(得分:2)
您可以尝试本教程使用提醒对话框创建搜索对话框 Click here
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Search Box");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
答案 2 :(得分:2)
在layout
文件夹
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
<Button
android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
此类用于处理对话框
class dialog extends Dialog {
private StackQuestionActivity activity;
private Button search, cancel;
private EditText text;
private dialog thisDialog;
public dialog(StackQuestionActivity context) {
super(context);
// TODO Auto-generated constructor stub
this.activity = context;
this.thisDialog = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialoge);
getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
initalize();
}
private void initalize() {
// TODO Auto-generated method stub
text = (EditText) findViewById(R.id.text);
search = (Button) findViewById(R.id.search);
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
thisDialog.cancel();
}
});
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String textString = text.getText().toString();
activity.searchWord(textString);
}
});
}
}
这是您的主要活动,按下按钮时调用对话框类:
package com.example.question;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class StackQuestionActivity extends Activity {
/** Called when the activity is first created. */
private Button press;
private StackQuestionActivity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.activity = this;
setContentView(R.layout.main);
press = (Button) findViewById(R.id.button);
press.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog yourDialog = new dialog(activity);
yourDialog.show();
}
});
}
public void searchWord(String textString) {
// TODO Auto-generated method stub
}
}
这是结果