需要在listView项目上单击实现自定义布局警报对话框

时间:2014-03-17 05:39:11

标签: android listview

我需要建议。

我有一个包含项目的列表视图。我需要以这样一种方式实现onitemclick,它要求输入一个值,并且该项目中也显示了值(我点击了它)。 listview中的每一行都包含一个TextView,并且应该在单击相应的行时更新值。

我在片段类中使用listview。

我没有任何想法。我需要建议或想法。

提前致谢。

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



        Toast.makeText(getActivity(), "pressed" , Toast.LENGTH_SHORT).show();

    }

如何从具有EditText“大小”和“确定”按钮的自定义对话框中获取值。此值应更新为列表。

4 个答案:

答案 0 :(得分:1)

我的建议是DialogFragment和一个简单的自定义监听器。

  1. 对于自定义对话框,请使用diaglogFragment.show();
  2. 中的onListItemClick
  3. 要返回值/更新listView,请在DialogFragment中添加一个由其调用Fragment实现的侦听器。

答案 1 :(得分:1)

在xml中创建自定义对话框并在onitemclick中显示对话框以输入值。

尝试this code

答案 2 :(得分:0)

使用此:

public class DialogAlert extends Dialog {
    Context mContext;
    Listeners mListeners;
    String mTitle;
    String mMessage;

    public DialogAlert (Context mContext, Listeners mListeners, String mTitle,
            String mMessage) {
        // TODO Auto-generated constructor stub
        super(mContext);
        this.mContext = mContext;
        this.mListeners = mListeners;
        this.mTitle = mTitle;
        this.mMessage = mMessage;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        setContentView(R.layout.dialog_alert);
        setCancelable(false);
        Typeface mTypeface = Typeface.createFromAsset(mContext.getAssets(),
                "fonts/helvetica.otf");
        TextView mTitle = (TextView) findViewById(R.id.confirm_title);
        TextView mMessage = (TextView) findViewById(R.id.confirm_message);
        mTitle.setText(this.mTitle);
        mMessage.setText(this.mMessage);
        mTitle.setTypeface(mTypeface);
        mMessage.setTypeface(mTypeface);

        Button confirmOK = (Button) findViewById(R.id.confirm_ok);
        confirmOK.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mListeners.OnClickOk();
            }
        });
        Button dialogClose = (Button) findViewById(R.id.confirm_close);
        dialogClose.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View paramView) {
                // TODO Auto-generated method stub
                mListeners.OnClickClose();
            }
        });
        Button dialogCancel = (Button) findViewById(R.id.confirm_cancel);
        dialogCancel
                .setOnClickListener(new android.view.View.OnClickListener() {

                    @Override
                    public void onClick(View paramView) {
                        // TODO Auto-generated method stub
                        mListeners.OnClickCancel();
                    }
                });
        confirmOK.setTypeface(mTypeface);
        dialogCancel.setTypeface(mTypeface);
    }

    public interface Listeners {
        void OnClickClose();

        void OnClickCancel();

        void OnClickOk();
    }

XML文件:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/close_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/dialog_left" />

    <Button
        android:id="@+id/confirm_close"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="17dp"
        android:background="@null"
        android:drawableTop="@drawable/dialog_close" />
</RelativeLayout>

<TableLayout
    android:id="@+id/edit_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/confirm_title"
    android:layout_margin="5dp"
    android:layout_marginLeft="75dp"
    android:minWidth="200dp"
    android:layout_toRightOf="@+id/close_container" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </TableRow>
</TableLayout>

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/close_container"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="30dp"
    android:src="@drawable/dialog_alert" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/confirm_message"
    android:layout_alignTop="@+id/imageView2"
    android:layout_toRightOf="@+id/close_container"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/confirm_ok"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/button_alert"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_weight="1"
        android:text="OK"
        android:textColor="#ffffff"
        android:textStyle="bold" />

    <Button
        android:id="@+id/confirm_cancel"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/button_alert"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
         android:layout_weight="1"
        android:text="CANCEL"
        android:textColor="#ffffff"
        android:textStyle="bold" />
</LinearLayout>

<TextView
    android:id="@+id/confirm_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/close_container"
    android:layout_alignLeft="@+id/edit_container"
    android:layout_marginBottom="40dp"
    android:text="Are You Sure ?"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/color_white"
    android:textSize="30sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/confirm_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/confirm_title"
    android:layout_centerVertical="true"
    android:text="Are you Sure you want to exit dude?"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/color_white"
    android:textSize="20sp"
    android:textStyle="bold" />

答案 3 :(得分:0)

检查Dialogfragment班级

它提供了与活动

等自定义设置的对话框

这里是如何使用它的例子

public class Dialog extends DialogFragment implements View.OnClickListener {

    private TextView messageText;

    private Button okButton;

    private String title;
    private String message;

    public Dialog(String title,String message) {
        this.title = title;
        this.message = message;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog, container);

        messageText = (TextView) view.findViewById(R.id.dialog_message);
        okButton = (Button) view.findViewById(R.id.dialog_ok);

        getDialog().setTitle(title);

        messageText.setText(message);
        okButton.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }
}

对话框的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginRight="10dp"
              android:layout_marginLeft="10dp">


    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/dialog_message"
            android:textAppearance="?android:textAppearanceMedium"/>

    <Button
            android:id="@+id/dialog_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceMedium"
            android:text="@string/ok"
            android:background="@drawable/button_blue"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:gravity="center"/>

</LinearLayout>

您可以使用此代码

显示对话框
Dialog d = new Dialog();
d.show(getSupportFragmetManager(), "Tag");

如果您的应用需要支持2.2之前的Android版本,则需要将appcompat库或sherlock库添加到项目中