android按钮点击事件对我不起作用

android按钮点击事件对我不起作用,android,button,android-relativelayout,Android,Button,Android Relativelayout,我有一个相对布局的android应用程序,我在其中放置了一个按钮控件,如下所示 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"

我有一个相对布局的android应用程序,我在其中放置了一个按钮控件,如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >    
<Button
        android:id="@+id/btnCalc"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="708dp"
        android:layout_marginLeft="50dp"
        android:text="@string/btncalc" />
</RelativeLayout>
btncalcu=(Button)findViewById(R.id.btnCalc);
        btncalcu.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();
                //calc();
            }

        });

问题是,当我运行应用程序时,没有错误或任何异常,但也没有显示Toast的任何内容。任何人都可以猜到可能出现的问题。请提供帮助

试试这种方法,看看是否有效

    btncalcu.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();

        }
    });
并确保已导入以下内容

import android.view.View.OnClickListener;
使用YourActivity.this作为上下文,不要使用getBaseContext

上下文用于告诉应用程序在何处显示Toast,因此建议使用当前活动。

不要使用getBaseContext,而是使用您拥有的上下文v.getContext

决议:

Toast.makeText(v.getContext(), "a", Toast.LENGTH_LONG).show();
这样使用:

btncalcu..setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             Toast.makeText(getApplicationContext(), "Clicked Button", Toast.LENGTH_LONG).show();

            // or

             Toast.makeText(YourActivityname.this, "Clicked Button",, Toast.LENGTH_LONG).show();
        }
    });

适合我。

请尝试以下方法:

并确保已导入以下内容

导入android.view.view.OnClickListener

试试这个

btncalcu=(Button)findViewById(R.id.btnCalc);
btncalcu.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "Calculation Button clicked", Toast.LENGTH_LONG).show();
    }
});
并导入此行

import android.view.View.OnClickListener;
要解决此问题,请执行以下操作:

一,

二,

并且必须检查您是否已导入此导入:

android.view.View.OnClickListener;
使用View.OnClickListener而不是Button.OnClickListener。它会解决你的问题


此外,getBaseContext无疑也适用于您,但最好使用ActivityName.this来进行祝酒,以防止任何内存泄漏。

如果您使用的是listView检查,请检查是否对其应用了任何提升。我也这么做了,在listView的顶部有一个浮动按钮,因此按钮从未被点击过。除非检查xml文件,否则无法识别。我写的代码仅供您参考

移除立面并进行onClick。这很合乎逻辑,在你这么做之后就可以工作了

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


    <ListView
        android:id="@+id/fullListViewToDo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#F3F3F3"
        android:dividerHeight="8.0sp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:drawSelectorOnTop="false"
        android:elevation="8dp">

    </ListView>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/todo_fragmentContainer">

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" />

</RelativeLayout>
只要一个小费

检查按钮是否启用

在xml中

android:enabled=false

android:enabled=true


您可以在这里添加完整的活动类代码吗?仅使用此代码而不是GetBaseContext您是否在Android清单文件中声明了活动?有时,我们忘记声明活动,而按钮单击无效。您是否尝试将baseContext更改为v.getContext?@AvinashKumarPankaj Anytime Bro!
import android.view.View.OnClickListener;
btncalcu=(Button)findViewById(R.id.btnCalc);
        btncalcu.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v)
            {
                Toast.makeText(YourActivityname.this, "a", Toast.LENGTH_LONG).show();
                //calc();
            }

        });
  btncalcu=(Button)findViewById(R.id.btnCalc);
            btncalcu.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();
                    //calc();
                }

            });
android.view.View.OnClickListener;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:id="@+id/fullListViewToDo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#F3F3F3"
        android:dividerHeight="8.0sp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:drawSelectorOnTop="false"
        android:elevation="8dp">

    </ListView>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/todo_fragmentContainer">

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" />

</RelativeLayout>