5

我有一个带有列表的主要活动和一个接收关键字以在数据库中进行搜索的方法。

我想实现AlertDialog一个搜索框,当我通过此表单中的动作侦听器单击按钮时,该搜索框应出现在主活动上

OnClickListener searchListener = new OnClickListener() {

    public void onClick(View v) {
        // ...
    }
};

这个对话框应该有一个简单的EditText框和两个按钮(SearchCancel),并且必须将所String选择的传递EditText给表单中的搜索方法:

public void searchWord(String keyword){
    // ....search in DB
    // ...updateListGUIWithNewValues()
}

此方法在主活动中获取新的列表值,以便活动可以更新 GUI 列表。

我已经实现了搜索方法和主要活动,但我不知道实现这种对话框的正确方法。

4

3 回答 3

6

这是我的警报对话框代码,其中包含编辑文本。我在按钮单击侦听器中调用它,因此它不是一个单独的方法。我上周在这个网站的某个地方找到了它,但我似乎找不到它,所以我可以引用它。

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();
于 2013-02-28T20:03:23.633 回答
2

嗨,试试本教程以使用警报对话框创建搜索对话框单击此处

  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();
于 2013-02-28T20:10:26.650 回答
2

对话框 XML

layout在您的文件夹中构建此 xml

<?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

    }
}

这是结果 查看

于 2013-02-28T20:17:46.353 回答