【问题标题】:Howto add onclick event to button in listview added by addheaderView?如何将 onclick 事件添加到通过添加标题视图添加的列表视图中的按钮?
【发布时间】:2012-03-02 18:14:25
【问题描述】:

我正在使用列表视图。在将主列表项添加到列表视图之前,我使用 addheaderview(v) 将标题添加到列表视图。标题有两个按钮。

在 logcat..它给出了问题 btnhome.setOnClickListener(new View.OnClickListener() {

它会强制关闭。 请帮我看看下面的代码哪里出错了。

等待您的回复 提前谢谢

enter code here

public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.games_by_category);

        Global globalclass=((Global)getApplicationContext());
        globalclass.setpageno(0);                   
        lstview =(ListView)findViewById(android.R.id.list);      

        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);            
        View v = inflater.inflate(R.layout.header, null);
        //View v = getLayoutInflater().inflate(R.layout.header, null);
        v.setMinimumHeight(10);
        lstview.addHeaderView(v);

        btnhome=(Button)findViewById(R.id.btnHome);
        btnhome.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) 
            {
                btnhome.setTextColor(Color.BLACK);
                Global globalclass=((Global)getApplicationContext());
                Intent inte=new Intent(GamesByCatActivity.this,HotGamesActivity.class);
                startActivity(inte);                    
          }
        });

【问题讨论】:

    标签: android


    【解决方案1】:

    只需从标题视图中获取按钮并设置 onClickListener。

    View header;
    Button headerButton = header.findViewById(R.id.btn1);
    headerButton.setOnClickListener(this);
    listView.addHeaderView(header);
    

    【讨论】:

      【解决方案2】:

      我无法理解您的问题。你想让我做什么?如果你有ListView 和标题,那么你使用LinearLayout:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="match_parent"
          android:layout_height="match_parent">
          <ListView android:layout_width="fill_parent" android:layout_height="wrap_content"  android:id="@+id/_lv_babies" >
          </ListView>
                <RelativeLayout  android:layout_width="fill_parent"
              android:layout_height="wrap_content" android:background="@color/blue_email"
              >
              <Button android:layout_width="wrap_content"
                  android:layout_height="wrap_content" 
                  android:text="Edit" android:textSize="15dp" android:padding="10dp"
                  android:layout_margin="5dp" android:textColor="@color/black"
                  android:id="@+id/_btn_edit"/>
              <Button android:layout_width="wrap_content"
                  android:layout_height="wrap_content" 
                  android:text="Add" android:textSize="15dp" android:padding="10dp"
                  android:layout_margin="5dp" android:textColor="@color/black"
                   android:id="@+id/_btn_add"/>
      
      </RelativeLayout>
      </LinearLayout>
      

      这是您可以这样使用的示例。

      【讨论】:

      • hii thx for reply.. 我在我的列表中添加了一个 addheaderview。 n那个标题有按钮。我想要这些按钮上的 onclick 事件。我该怎么做。
      【解决方案3】:
      For your button click....if its works then tick on right click or accept my answer. 
      protected void setXmlComponents(){
              _btn_add =(Button)findViewById(R.id._btn_add);
              _btn_edit =(Button)findViewById(R.id._btn_edit);
                  }
          protected void setListener(){
              _btn_add.setOnClickListener(new OnClickListener() {
      
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                                  }
              });
            _btn_edit.setOnClickListener(new OnClickListener() {
      
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
      
                  }
              });
      
          }
      

      【讨论】:

      • 这不是我的问题的解决方案:(
      • 将其包含在列表视图下方的 xml 中 编辑下面给出的代码 btnhome=(Button)findViewById(R.id.btnHome); btnhome.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } });
      • 我添加了这个,但它仍然强制关闭.. :(
      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多