【问题标题】:Click on a button inside a dialog which is pop-up from a gridview when i click a item.And this gridview is inside a fragment单击一个对话框内的一个按钮,当我单击一个项目时,该对话框从一个网格视图中弹出。这个网格视图在一个片段内
【发布时间】:2014-06-22 02:56:52
【问题描述】:

当我单击一个项目时,如何单击从 gridview 弹出的对话框内的按钮。这个 gridview 在片段内?

它不允许我为这个确定按钮创建 onclicklistener。 错误提示: View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数(new DialogInterface.OnClickListener(){})

这是我的代码:

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MotionEventCompat;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DiningFragment extends Fragment{


    GridView gridView;
    TextView Details1;
    TextView Details2;
    static final String[] NAMES = new String[] { "aroma", "aura", "crave",
            "urbanbean", "flava", "map" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View inflatedView = inflater.inflate(R.layout.dining, container,
                false);


        Details1 = (TextView) inflatedView.findViewById(R.id.dining_details1);
        Details2 = (TextView) inflatedView.findViewById(R.id.dining_details2);
        gridView = (GridView) inflatedView.findViewById(R.id.gridView);

        gridView.setAdapter(new DiningAdapter(getActivity(), NAMES));

        String textLinks1 = "tesetestsetest";
        String textLinks2 = "testtest";

        if (Details1 != null) {
            // set text to text view
            Details1.setText(textLinks1);

            Linkify.addLinks(Details1, Linkify.ALL); // linkify all links in
                                                        // text.
            // you can set link color with the help of text view property
            Details1.setLinkTextColor(Color.BLUE);

        }
        if (Details2 != null) {
            // set text to text view
            Details2.setText(textLinks2);

            Linkify.addLinks(Details2, Linkify.ALL); // linkify all links in
                                                        // text.
            // you can set link color with the help of text view property
            Details2.setLinkTextColor(Color.BLUE);

        }

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {




                Toast.makeText(
                        getActivity().getApplicationContext(),
                        ((TextView) v.findViewById(R.id.dining_label))
                                .getText(), Toast.LENGTH_SHORT).show();







                **//  dialog
                final Dialog dialog = new Dialog(getActivity());
                dialog.setContentView(R.layout.dining_dialog);
                dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.dining_dialogtext);
                text.setText("Android custom dialog example!");
                ImageView image = (ImageView) dialog.findViewById(R.id.dining_dialogimage);
                image.setImageResource(R.drawable.ic_launcher);
                Button dialogButton = (Button) dialog.findViewById(R.id.dining_dialogButtonOK);


                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });


                dialog.show();**









            }
        });

        return inflatedView;
    }



}

【问题讨论】:

    标签: android android-layout gridview android-fragments android-dialog


    【解决方案1】:

    我实现的 OnClickListener 不正确,尝试实现 View.OnClickListener 而不是 DialogInterface.OnClickListener。

    使用导入

    导入 View.OnClickListener

    而不是

    导入 DialogInterface.OnClickListener

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多