在我的应用中,我有三个标签。在每个标签中,我有一些控件。当我尝试为我的按钮实现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>
我做错了什么?
答案 0 :(得分:0)
在onCreateView(..)
的{{1}}中定义您的观点,并按照以下方式注册听众:
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;
}
和onClickListener
界面:
onItemClickListener
通过使用这些界面,您将被迫覆盖public class ContactPage extends Fragment implements onClickListener, onItemClickListener {
和onClick(View v)
,您可以在其中处理按钮和列表的操作。
答案 1 :(得分: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);
}});
}