简体   繁体   English

使用ListActivity的TabSpec ...如何为Button设置OnClickListener?

[英]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. 在TabHost中,我正在设置一个TabSpec,它包含一个带有几个按钮的标题区域,下面是一个ListView。 The Activity to manage this TabSpec is defined as "extends ListActivity". 管理此TabSpec的活动定义为“扩展ListActivity”。 However, now I'm running into the problem of not being able to define an OnClickListener to check for the submit button pressed. 但是,现在我遇到了无法定义OnClickListener来检查按下的提交按钮的问题。 How can I solve that? 我怎么解决这个问题?

Trying to cast the Button by 试图按下按钮

btnRatingSubmit.setOnClickListener((OnClickListener) this);

raises an ClassCastException... 引发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); 它应该不是问题,如果你setContentView(R.layout.your_layout); and get the instance of btnSubmit . 并获取btnSubmit的实例。 But if you are facing the problem so better just change your ListActivity to Activity . 但是,如果您面临问题,那么只需将ListActivity更改为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). 我认为你的问题是((OnClickListener)这个)。 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? 但是,您是否在包含此按钮的Activity上实现OnClickListener? Otherwise, try casting like this ((View.OnClickListener) this). 否则,尝试像这样投射((View.OnClickListener)this)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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