android - ImageView onClick 不工作 FragmentActivity

标签 android

可能是这么简单,但不知道我已经尝试了这么多,但它不起作用:(

我有这样的布局

enter image description here

我有以下 java 代码

public class MainActivity extends FragmentActivity{
private ImageView ivMainMenu;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ivMainMenu = (ImageView) findViewById(R.id.ivMainMenu);
    ivMainMenu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("showSlider clicked");
        }
    });
}

我试过了

 android:clickable="true" 

还是不行

下面是我的xml代码

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/rlMainHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/header_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvHeaderText"
        style="@style/header_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/txtScore"
        style="@style/black_large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="@dimen/extra_small"
        android:background="@drawable/keyboard_btn"
        android:padding="@dimen/extra_small"
        android:text="@string/finish"
        android:textColor="@color/dark_text_color"
        android:textStyle="bold"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/ivMainMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:src="@drawable/menu" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/rlMainHeader"
    android:background="@color/main_bg"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/flMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <LinearLayout
        android:id="@+id/llMainSlider"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:visibility="visible" >

        <include
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            layout="@layout/slider" />

        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/dark_text_color"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

在我的 logcat 中,每当我点击 imageview 时,我都会收到以下消息

    04-27 11:09:42.996: V/Provider/Settings(1018):  from settings cache , name = sound_effects_enabled , value = 0

我还想让你知道,如果我在 flMain 中加载任何 fragment ,在该 fragment 中所有事件都工作正常但在主布局上没有 :( 同样在 llMainSlider 中注意到正在发生 :(

最佳答案

您为什么不尝试在 XML 上声明事件? 就这么简单

    android:id="@+id/ivMainMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:onClick="myNewMethod" <!--add this extra line!--->
    android:src="@drawable/menu" />

然后简单地在您的 Main Activity 上添加 myNewMethod 作为这样的额外功能

public class MainActivity extends FragmentActivity{
private ImageView ivMainMenu;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void myNewMethod(View v) {
      System.out.println("showSlider clicked");
}

祝你好运;)

关于android - ImageView onClick 不工作 FragmentActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23319666/

相关文章:

java - 验证时出错

android - callApplicationOnCreate 被调用但不是 newApplication(扩展 AndroidJUnitRunner)

java - 如何在android studio中创建数据库。因为我写的代码没有在 ddms 中创建数据库

java - 将多维数组传递给另一个 Activity

Android:在丢失的蓝牙连接上完成多项 Activity

java - 访问保存到 Android 设备内部存储的数据

android - 使用 AppCompat/Material 主题膨胀类 com.google.android.material.appbar.AppBarLayout 时出错

android - 小部件。 Activity 泄露了 IntentReceiver

android - RecyclerView 不要在角落夹住项目

java - ArrayAdapter 中的 getView() 未从 AsyncTask 调用