简体   繁体   中英

how to make a click event on a textview of child of a custom listview in android

Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews , one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextView s should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected. here is the screenshot of the my listview

https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706

You can do it in two ways:

1. Either by directly using onClickListener like this:

textView.setOnClickListener(new View.OnClickListener(
        public void onClick(View arg0) {

            // Do anything here.

        }
    });

OR

2. In XML file, in declaration of <TextView /> add one more attribute as:

android:onClick="onClickTextView"

and in yout activity, add this function:

public void onClickTextView(View view) {

         // Do anything here.
}

UPDATE:

Use following code to get click event on TextView:

// Click event for single list row
        getListView().setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView tv = (TextView) (findViewById(R.id.title));
                if (tv != null) {
                    tv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this, "CLICKED",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                } else {
                    Toast.makeText(MainActivity.this, "TV not found",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

Assume you extend BaseAdapter to set the listview content ->

Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.

Try this :

When you select one textview the other three will be unclickable

    final TextView texta = (TextView) findViewById(R.id.text_a);
    final TextView textb = (TextView) findViewById(R.id.text_b);
    final TextView textc = (TextView) findViewById(R.id.text_c);
    final TextView textd = (TextView) findViewById(R.id.text_d);
    texta.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) {

            textb.setClickable(false);
            textc.setClickable(false);
            textd.setClickable(false);
        }

    });
    textb.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) {

            texta.setClickable(false);
            textc.setClickable(false);
            textd.setClickable(false);
        }

    });
    textc.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) {

            texta.setClickable(false);
            textb.setClickable(false);
            textd.setClickable(false);
        }

    });
    textd.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) {

            texta.setClickable(false);
            textb.setClickable(false);
            textc.setClickable(false);
        }

    });

It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.

Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.

For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them). You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.

Layout could be something like this :
在此处输入图片说明

Follow these links for reference: for listView
for RadioGroup

I hope this will help

Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.

either use this in adapter class as by Shrikant:

textViewa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do anything here.
        }
    });

    textViewb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            // Do anything here.
        }
    });

    //and so on...

    // or better to use ViewHolder holder; for these type of listviews;

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Do what you want to do.

            // for my i have to call a method in my parent activity. so in    constructor of adapter, I passed the activity and then typecasted it like

            ParentActivity parent = (ParentActivity) activity;
            parent.chosenAnswer(view.getId());

            // then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.

            // OR 
            //you can write like this too..

            switch (view.getId()) {
                case R.id.textViewa:
                    break;
                case R.id.textViewb:
                    break;
                case R.id.textViewc:
                    break;
                case R.id.textViewd:
                    break;
            }
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM