开发者

Clickable TextView only works once

开发者 https://www.devze.com 2023-03-10 07:48 出处:网络
I made a textview clickable then it triggers an intent, it works but only once. After clicking the textview the first time it\'s no longer clickable and I have no idea why. Your help will be appreciat

I made a textview clickable then it triggers an intent, it works but only once. After clicking the textview the first time it's no longer clickable and I have no idea why. Your help will be appreciated.

<TextView android:text="Click Me" android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/t开发者_JAVA百科extView1"
android:textSize="50dp" android:focusable="false" android:longClickable="true"></TextView>

TextView txt = (TextView) findViewById(R.id.textView1);

    txt.setOnLongClickListener(new OnLongClickListener() {

        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Example.this, Alert.class));

            return false;
        }
    });


What does the alert class do? If it is an activity it could be that it is being laid over the top of your current activity so although you can see your activity, it's not at the top of the stack/in the foreground so you're not actually pressing the TextView, you're pressing a transparent activity that is over the top of it.

The easiest way to check that is to press the TextView, then press your device's back key and see if the TextView responds to the click.

Or are you sure you're not setting the same layout in Alert.class? That would make it look like it's the same activity but if the Alert class doesn't set the click listener, nothing is going to happen.

The fact that you're starting an activity with an intent and that's making an instance of another class (which I assume is also an activity) stops the click working to me is seriously suggesting that Alert is getting the click somehow instead of Example. When you say things work fine if you remove the intent backs that up as well. Maybe you could post the full source of both classes?


Do you have an onClickListener that disables the textview? By returning false from onLongClick Android would also invoke the onClick listener if you have one.

Also you could try to remove android:focusable="false"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号