【问题标题】:TabSpec with ListActivity … how to set OnClickListener for Button?TabSpec with ListActivity ... 如何为 Button 设置 OnClickListener?
【发布时间】:2012-06-06 04:45:49
【问题描述】:

在 TabHost 中,我设置了一个 TabSpec,它包含一个带有几个按钮的标题区域,下面是一个 ListView。管理这个 TabSpec 的 Activity 被定义为“扩展 ListActivity”。 但是,现在我遇到了无法定义 OnClickListener 来检查是否按下提交按钮的问题。我该如何解决?

尝试通过

投射按钮
btnRatingSubmit.setOnClickListener((OnClickListener) this);

引发 ClassCastException...

这是布局的基本摘录:

<?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>

【问题讨论】:

    标签: android android-layout casting android-listview


    【解决方案1】:

    代替

    btnRatingSubmit.setOnClickListener((OnClickListener) this);
    

    试试这个:

    btnRatingSubmit.setOnClickListener(new View.OnClickListener(){
    
    public void onClick(View v) {
                // TODO Auto-generated method stub
    
            }
        }); 
    

    【讨论】:

      【解决方案2】:

      如果您setContentView(R.layout.your_layout); 并获得btnSubmit 的实例,这应该不是问题。但是,如果您遇到问题,最好将您的 ListActivity 更改为 Activity

      【讨论】:

        【解决方案3】:

        我认为您的问题在于 ((OnClickListener) this)。如果没有更多关于这个类的代码,我无法准确地判断出了什么问题。但是,您是否在拥有此按钮的 Activity 上实现 OnClickListener?否则,请尝试像这样进行投射 ((View.OnClickListener) this)。

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-28
            • 2016-01-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多