1

在我的应用程序中,我有三个选项卡。在每个选项卡中,我都有一些控件。当我尝试为我的按钮实现 onClick 方法时,我发现按钮没有响应手势,更具体地说,是点击。

这是选项卡的类:

package com.telkitty.myPetProject

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.provider.Contacts;
import android.provider.ContactsContract;

public class ContactPage extends Fragment {

    private static final int CONTACT_PICKER_RESULT = 1001;  

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Activity activity = getActivity();

        if (activity != null) 
        {       
            addListenerOnButton();      
        }   
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.contact_page, container, false);       
        return view;
    }


    public void onListItemClick(ListView l, View v, int position, long id) {
        Activity activity = getActivity();


    }


    public void addListenerOnButton() 
    {   
        Button add = (Button) getView().findViewById(R.id.add_button);
        add.setOnClickListener(new View.OnClickListener()
        {
        public void onClick(View v)
        {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
                    Contacts.CONTENT_URI);  
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
        }});
    }

}

这是xml:

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


   <Button
        android:id="@+id/edit_button"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp"
        android:text="@string/edit_contact"/>

   <Button
        android:id="@+id/add_button"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:text="@string/add_contact"/>



    <ListView
        android:id="@+id/contact_list"
        android:layout_width="320dp"
        android:layout_height="380dp"
        android:paddingTop="50dp" >

    </ListView>    

</RelativeLayout>

我做错了什么?

4

2 回答 2

0

onCreateView(..)在您的中定义您的视图Fragment并注册监听器,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.contact_page, container, false);
        Button lEditButton = (Button) view.findViewById(R.id.edit_button);
        Button lAddButton = (Button) view.findViewById(R.id.add_button);
        ListView lContactList = (ListView) view.findViewById(R.id.contact_list);

        lEditButton.setOnClickListener(this);
        lAddButton.setOnClickListener(this);
        lContactList.setOnItemClickListener(this);

        return view;
    }

实现onClickListeneronItemClickListener接口:

public class ContactPage extends Fragment implements onClickListener, onItemClickListener {

通过使用这些界面,您将被迫覆盖onClick(View v)以及onItemClick(..)您可以在哪里处理按钮和列表的操作。

于 2013-03-14T14:36:18.943 回答
0

好的,只是碰巧我的按钮实际上位于 ListView 下方,因此尽管它们看起来好像在那里,但每次我单击它们时,实际上都选择了 ListView。

我添加了标记线,现在按钮可以正常工作了。

   public void addListenerOnButton() 
    {   
        Button add = (Button) getView().findViewById(R.id.add_button);
        add.bringToFront(); // <--- this line
        add.setOnClickListener(new View.OnClickListener()
        {
        public void onClick(View v)
        {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
                    Contacts.CONTENT_URI);  
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
        }});
    }
于 2013-03-15T12:52:43.443 回答