`onClick`方法在Android Studio中不起作用。

huangapple go评论80阅读模式
英文:

onClick method not working in android studio

问题

XML代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:clickable="true"
    android:onClick="ClickTournament_info"
    android:focusable="true">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="赛事信息"
        android:padding="12dp"
        android:layout_marginStart="16dp"/>

    <ImageView
        android:layout_marginTop="-10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:src="@drawable/tournament_info"/>
</LinearLayout>

Java代码如下:

public void ClickTournament_info(View view){
    redirectActivity(this, TournamentInfo.class);
}

public static void redirectActivity(Activity activity, Class aClass) {
    // 初始化意图
    Intent intent = new Intent(activity, aClass);
    // 设置标志
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // 启动活动
    activity.startActivity(intent);
}
英文:

I made a side navigation drawer and it works fine but whenever I click any option in navigation drawer nothing happens.It's like it is not taking any input. Onclicking any option I want to redirect user to a new activity but unfortunately it doesn't happens.

XML code is as follows

 &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:orientation=&quot;horizontal&quot;
        android:gravity=&quot;center_vertical&quot;
        android:clickable=&quot;true&quot;
        android:onClick=&quot;ClickTournament_info&quot;
        android:focusable=&quot;true&quot;&gt;

    &lt;TextView 
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_weight=&quot;1&quot;
        android:text=&quot;Tournament Info&quot;
        android:padding=&quot;12dp&quot;
        android:layout_marginStart=&quot;16dp&quot;/&gt;

    &lt;ImageView
        android:layout_marginTop=&quot;-10dp&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;48dp&quot;
        android:src=&quot;@drawable/tournament_info&quot;/&gt;

&lt;/LinearLayout&gt;

Java code is as follows

public void ClickTournament_info(View view){
        redirectActivity(this,TournamentInfo.class);

    }



public static void redirectActivity(Activity activity,Class aClass) {
        //Initialize intent
        Intent intent = new Intent(activity,aClass);
        //set flag
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //start activity
        activity.startActivity(intent);
    }

答案1

得分: 0

首先,尝试始终创建一个 OnclickListener,而不是在 XML 中添加 onclick 属性。

在你的 XML 中,为线性布局添加一个 ID 属性:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearlayout"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:clickable="true"
    android:onClick="ClickTournament_info"
    android:focusable="true">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Tournament Info"
        android:padding="12dp"
        android:layout_marginStart="16dp"/>

    <ImageView
        android:layout_marginTop="-10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:src="@drawable/tournament_info"/>
</LinearLayout>

现在在你的类文件的 onCreate 方法中:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
linearLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        redirectActivity(this, TournamentInfo.class);
    }
});
英文:

Firstly, try to always create an Onclicklistener instead of adding an onclick attribute in xml.

In your xml, add an ID attribute to your linear layout

&lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;

        android:id=&quot;@+id/linearlayout&quot;

        android:orientation=&quot;horizontal&quot;
        android:gravity=&quot;center_vertical&quot;
        android:clickable=&quot;true&quot;
        android:onClick=&quot;ClickTournament_info&quot;
        android:focusable=&quot;true&quot;&gt;

    &lt;TextView 
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_weight=&quot;1&quot;
        android:text=&quot;Tournament Info&quot;
        android:padding=&quot;12dp&quot;
        android:layout_marginStart=&quot;16dp&quot;/&gt;

    &lt;ImageView
        android:layout_marginTop=&quot;-10dp&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;48dp&quot;
        android:src=&quot;@drawable/tournament_info&quot;/&gt;

&lt;/LinearLayout&gt;

Now in your classfile inside the Oncreate Method,

LinearLayout LinearLayout = (LinearLayout )findViewById(R.id.linearlayout);
LinearLayout.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View v) {
        redirectActivity(this,TournamentInfo.class);
    }
});

答案2

得分: 0

我认为当您想要处理导航抽屉上的点击事件时,您需要使用onNavigationItemSelected方法。

了解更多:https://developer.android.com/reference/com/google/android/material/navigation/NavigationView.OnNavigationItemSelectedListener#onNavigationItemSelected(android.view.MenuItem)

英文:

I think you need to use the onNavigationItemSelected method when you want to handle click events on the navigation drawer.

Learn more: https://developer.android.com/reference/com/google/android/material/navigation/NavigationView.OnNavigationItemSelectedListener#onNavigationItemSelected(android.view.MenuItem)

答案3

得分: 0

你已经有一个onClick方法。仅从该方法开始导航。
尝试像这样,
`onClick`方法在Android Studio中不起作用。

英文:

You already have an onClick method. start navigating from that method only.
Try like this,
`onClick`方法在Android Studio中不起作用。

huangapple
  • 本文由 发表于 2020年9月8日 19:44:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63793193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定