onLongClickListener问题问题、onLongClickListener

2023-09-03 21:29:58 作者:流逝i

这code是从我的BaseAdapter局部code相适应我的可扩展列表视图。

This code is a partial code from my BaseAdapter which adapts my expandable listview.

这是应用在一般的可扩展的ListView。

This application is in general an Expandable ListView.

我的目标是设定的TextView颜色为蓝色,一旦用户点击长

My goal is to set textview color to blue once the user has long clicked

在我的列表视图的父布局我有两个字符串,该持有人解决他们。

in my listview's parent layout I got 2 strings , the holder is addressing them both.

我设法上所需位置设置一个onClickListener和设置所需的功能。 事情是,现在我需要同时进行两个功能: 当龙已点击: 1.复制到剪贴板(孩子的情况下) 2.复制母公司的TextView的行画,以一个颜色,以表示该行被复制

I managed to set an onClickListener on the desired location and to set the desired function. Thing is, now I need 2 functions to occur simultaneously: When Long Clicked: 1. Copy to clipboard (the child's context) 2. paint the copied parent textview's row to a color to indicate that row was copied

问题现在的问题是,我无法搞清楚如何实现。 我也知道了$ C $下设置的TextView的背景色

Problem right now is I'm having trouble figuring out how to implement that. I also know the code for setting the textview's bg color

的TextView.setBackgroundColor(Color.parseColor(#FF5555));  TextView的.setHighlightColor(Color.BLUE);

"TextView".setBackgroundColor(Color.parseColor("#FF5555")); "TextView".setHighlightColor(Color.BLUE);

等。

在此先感谢任何响应/帮助/辅助,欢呼声。

thanks in advance for any response/help/assist , cheers.

static class ViewHolder {
    TextView rname_parent;
    TextView city_parent;
}

    @Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView) {
    Group group = (Group) getGroup(parent);
        ViewHolder holder;
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.parent_layout, parentView, false);
    //}

        //convertView.setOnLongClickListener(new LongClick(group.getName()));
        holder = new ViewHolder();
        holder.rname_parent = (TextView) convertView.findViewById(R.id.rname_parent_layout);
        holder.city_parent = (TextView) convertView.findViewById(R.id.city_parent_layout);

        convertView.setTag(holder);
    }
     else {
        holder = (ViewHolder) convertView.getTag();
    }

        holder.rname_parent.setText(group.getName());
        holder.rname_parent.setTypeface(null, Typeface.BOLD_ITALIC);
        holder.rname_parent.setTextSize(27);
        holder.city_parent.setText(group.getCity());

    return convertView;
}

//TODO ########################################################################
//TODO ##############this.content = The clipBoard String#######################
//TODO ########################################################################
    @SuppressWarnings("deprecation")

    class  LongClick implements View.OnLongClickListener {
        private String content;
        public  LongClick (String content,String c2, String c3, String c4, String c5,String c6,String c7,String c8,String c9) {
            this.content = content+" "+c2+content;

        }

         // constructor used to obtain the item's valu
        @Override
        public  boolean  onLongClick (View  View){
            ClipboardManager clipboard = (ClipboardManager)
                    View.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            // Scrapbook Manager
            clipboard.setText(content);

            // set the value of the scrapbook item of
            Toast mToast = Toast.makeText(View.getContext(), "Copy to the Clipboard", Toast.LENGTH_SHORT);
            mToast.show();
            return  true ;
        }
    }


    //this method will return a view for each sub category

    @Override
    public View getChildView(final int parent, int childPosition, boolean LastChild, View convertView, ViewGroup parentView) {
        Child child = (Child) getChild(parent, childPosition);
        ArrayList<Child> ch = getAllchildren(parent);

        // if there is no current view for the specific child this will create it
        if(convertView == null){
            LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.child_layout, parentView,false);
        }
       convertView.setOnLongClickListener(new LongClick(ch.get(0).getName(), ch.get(1).getName(), ch.get(2).getName(), ch.get(3).getName(), ch.get(4).getName(), ch.get(5).getName(), ch.get(6).getName(), ch.get(7).getName(), ch.get(8).getName()));
/*
        convertView.isClickable();
        convertView.setLongClickable(true);
        convertView.setOnClickListener(new View.OnClickListener() {
        ArrayList<Child> ch = getAllchildren(parent);

        @Override
        public void onClick(View v) {
            new LongClick(ch.get(0).getName(), ch.get(1).getName(), ch.get(2).getName(), ch.get(3).getName(), ch.get(4).getName(), ch.get(5).getName(), ch.get(6).getName(), ch.get(7).getName(), ch.get(8).getName());
        }
        //new LongClick(ch.get(0).getName(), ch.get(1).getName(), ch.get(2).getName(), ch.get(3).getName(), ch.get(4).getName(), ch.get(5).getName(), ch.get(6).getName(), ch.get(7).getName(), ch.get(8).getName())
        }
        );
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            ArrayList<Child> ch = getAllchildren(parent);
            @Override
            public boolean onLongClick(View v) {
                new LongClick(ch.get(0).getName(), ch.get(1).getName(), ch.get(2).getName(), ch.get(3).getName(), ch.get(4).getName(), ch.get(5).getName(), ch.get(6).getName(), ch.get(7).getName(), ch.get(8).getName());
                return true;
            }
        });
*/
        TextView child_textview = (TextView) convertView.findViewById(R.id.child_txt);
        child_textview.setText(child.getName().toString());

        return convertView;
    }

试用TBridges的解决方案,增加了执行和错误的logcat,显然我正在做一些事情错了,显然我不是解决OnLongClick以确切的TextView:/

Tried out TBridges's solution, added the implementation and the logcat error, clearly I'm doing some thing wrong, apparently I'm not addressing the OnLongClick view to the exact TextView :/

logcat的消息:

logcat message:

Attempt to invoke virtual method 'void android.widget.TextView.setBackgroundColor(int)' on a null object reference

新的实现:

@Override
        public  boolean  onLongClick (View  View){
            ClipboardManager clipboard = (ClipboardManager)
                    View.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            // Scrapbook Manager
            clipboard.setText(content);

            // set the value of the scrapbook item of
            Toast mToast = Toast.makeText(View.getContext(), "Copy to the Clipboard", Toast.LENGTH_SHORT);
            mToast.show();
            //ViewHolder holder;
           // holder = new ViewHolder();
            //holder.rname_parent = (TextView) View.findViewById(R.id.rname_parent_layout);
            TextView tv = (TextView) View.findViewById(R.id.rname_parent_layout);
            if (View instanceof TextView) {
                tv = (TextView) View;
            }
            tv.setBackgroundColor(Color.parseColor("#FF5555"));
            return  true ;
        }
    }

//吓到我了

//Freaking me out

推荐答案

onLongClick(),你应该能够做这样的事情:

In onLongClick() you should be able to do something like this:

Textview textView;
if (view instanceof TextView) {
    textView = (TextView) view;
}
// change text view's color