如何在列表项单击时弹出此对话框?

时间:2012-02-27 14:10:59

标签: android dialog

我得到了一项任务,我必须实现自定义呼叫转发系统,并且正在寻找这样做的方法。我决定Androids自己的呼叫转发看起来很棒,并决定我想提示用户点击列表视图上的项目。基本上我想在用户点击项目时弹出它。

this

这是我正在尝试实现对话框的代码:

public class CallForwardActivity extends ListActivity 
{
String[] settingsLabels = {"Viderestillinger", "Altid", "Optaget", "Ingen svar", "Timeout"};
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ListView lv = getListView();
  lv.setTextFilterEnabled(true);
  setListAdapter(new ArrayAdapter<String>(this, R.layout.callforward_items, R.id.callforward_item_text, settingsLabels));

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) 
    {
        // Open Dialog Box with an edittext view for setting the correct number to Call forward
        myTextDialog().show() // ???
    }
  });
}


private Dialog myTextDialog() {
    final View layout = View.inflate(this, R.layout.custom_dialog, null);

    final EditText savedText = ((EditText) layout.findViewById(R.id.myEditText));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(0);

    builder.setPositiveButton("Save", new Dialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
           String myTextString = savedText.getText().toString().trim();
        }
    });
    builder.setView(layout);
    return builder.create();
 }

如何“调用”此方法/显示对话框?

2 个答案:

答案 0 :(得分:1)

如果你想构建自己的对话框(例如在onItemClickListener中),那么AlertDialog.Builder是一个很好的起点。例如:

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) 
    {
        new AlertDialog.Builder(view.getContext())
            .setMessage("Something here")
            .setNegativeButton("Close", null).show();

    }
  });

您可以在此处查看AlertDialog的文档:http://developer.android.com/reference/android/app/AlertDialog.Builder.html

答案 1 :(得分:1)

以这种方式使用它来声明对话框

final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                         builder.setView(//your view)
                           .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog, final int id) {
                                //update click
                            }
                           })
                           .setNeutralButton("Disable", new DialogInterface.OnClickListener() {
                                public void onClick(final DialogInterface dialog, final int id) {
                                    //disable click
                                }
                               })
                           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                public void onClick(final DialogInterface dialog, final int id) {
                                //cancel click
                                }
                               });

并在您的点击监听器中显示如下,

builder.show();

在函数builder.setView()中,如果要使其与图形相同,则必须为其传递一个视图(例如布局),其中包含textview和edittext以及按钮。