点击我的列表项打开一个对话框。该对话框有2个图像视图和textview。问题是imageview未被点击。只有textview是可点击的。
以下是对话框的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/pledge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="@null"
android:src="@drawable/uncheck" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pledge"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/pledge"
android:gravity="center_vertical"
android:text="@string/pledge" />
<ImageView
android:id="@+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pledge"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="@null"
android:src="@drawable/uncheck" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pledge"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/points"
android:gravity="center_vertical"
android:text="@string/points" />
<TextView
android:id="@+id/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/points"
android:layout_centerInParent="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/confirm"
android:gravity="center"
android:text="Confirm" />
</RelativeLayout>
只有@+id/confirm
的TextView可以点击,但我想将上面定义的图片视图(@+id/pledge & @+id/points
)也可以点击。
以下是课程
public class ConfirmDialogFragment extends DialogFragment {
Boolean pointsChecked = false;
Boolean pledgeChecked = false;
ImageView pledge,points;
@Override
public void dismiss() {
// TODO Auto-generated method stub
super.dismiss();
}
public ConfirmDialogFragment() {
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
}
@SuppressLint("InlinedApi")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getActivity();
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.confirm_dialog_fragment,null);
dialog.setContentView(v);
final ImageView pledge = (ImageView) v
.findViewById(R.id.pledge);
pledge .setClickable(true);
final ImageView points = (ImageView) v
.findViewById(R.id.points);
points.setClickable(true);
TextView confirm = (TextView) v.findViewById(R.id.confirm);
confirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AppSession sInstance = AppSession.getInstance();
sInstance.setConfirm(true);
dismiss();
Fragment fragment = new ListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
if (fragmentManager .getBackStackEntryCount() > 0){
fragmentManager .popBackStack();
}
ft.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
}
});
pledge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (pledgeChecked) {
pledge.setBackgroundResource(R.drawable.uncheck);
pledgeChecked = false;
} else {
pledge.setBackgroundResource(R.drawable.check);
pledgeChecked = true;
}
}
});
points.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (pointsChecked) {
points.setBackgroundResource(R.drawable.uncheck);
pointsChecked = false;
} else {
points.setBackgroundResource(R.drawable.check);
pointsChecked = true;
}
}
});
return dialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
我尝试过很多选项,在xml中使clickable = true并在代码中设置clickable,但似乎没有任何效果。我不确定我是否遗漏了一些东西,因为我过去曾做过类似的事情,但现在却在某种程度上挣扎了很长时间。请指教。
答案 0 :(得分:2)
在XML文件中,您将图像设置为来源。然后,您通过更改背景以编程方式更改代码中的图像,但源仍将存在并完全隐藏背景。 尝试使用
points.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (pointsChecked) {
points.setImageResource(R.drawable.uncheck);
pointsChecked = false;
} else {
points.setImageResource(R.drawable.check);
pointsChecked = true;
}
}
});
编辑:通过自动生成的注释,我看到您正在使用Eclipse。我强烈建议您尝试使用Android Studio。我最近换了一个,我认为它更好。
答案 1 :(得分:0)
points.setBackgroundResource(R.drawable.check)