简体   繁体   中英

TabSpec with ListActivity … how to set OnClickListener for Button?

In a TabHost, I'm setting up a TabSpec that consists of a header area with a few buttons and below, a ListView. The Activity to manage this TabSpec is defined as "extends ListActivity". However, now I'm running into the problem of not being able to define an OnClickListener to check for the submit button pressed. How can I solve that?

Trying to cast the Button by

btnRatingSubmit.setOnClickListener((OnClickListener) this);

raises an ClassCastException...

Here is a basic excerpt of the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/edComment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine" >
</EditText>

<Button
    android:id="@+id/btnSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnSubmit" />

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

It should not be a problem, if you setContentView(R.layout.your_layout); and get the instance of btnSubmit . But if you are facing the problem so better just change your ListActivity to Activity .

Instead of

btnRatingSubmit.setOnClickListener((OnClickListener) this);

try this:

btnRatingSubmit.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    }); 

I think your issue is with ((OnClickListener) this). I can't exacly tell what is wrong without more code on this class. But, are you implementing OnClickListener on the Activity which holds this button? Otherwise, try casting like this ((View.OnClickListener) this).

您应该为活动实现View.OnClickListener以继续使用(OnClickListener),或者只是创建一个新的View.OnClickListener来替换“this”

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