如何在android studio中添加按钮点击事件

时间:2022-08-26 09:14:02

So I have done some research, and after defining you button as an object by the code

所以我做了一些研究,在通过代码将按钮定义为对象之后

private Button buttonname;
buttonname = (Button) findViewById(R.id.buttonnameinandroid) ;

here is my problem

这是我的问题

buttonname.setOnClickListener(this); //as I understand it, the "**this**" denotes the current `view(focus)` in the android program

then your OnClick() event...

那么你的OnClick()事件……

Problem:

问题:

When I type in the "this", it says:

当我输入“this”时,它说:

setOnClickListener (Android.View.view.OnClickListener) in View cannot be applied to (com.helloandroidstudio.MainActivity)

I have no idea why?

我不知道为什么?

here is the code from the .java file

下面是来自.java文件的代码。

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private Button btnClick;
    private EditText Name, Date;
    private TextView msg, NameOut, DateOut;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnClick = (Button) findViewById(R.id.button) ;
        btnClick.setOnClickListener(this);
        Name = (EditText) findViewById(R.id.textenter) ;
        Date = (EditText) findViewById(R.id.editText) ;
        msg = (TextView) findViewById(R.id.txtviewOut) ;
        NameOut = (TextView) findViewById(R.id.txtoutName) ;
        DateOut = (TextView) findViewById(R.id.txtOutDate) ;
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    public void onClick(View v)
    {
        if (v == btnClick)
        {
            if (Name.equals("") == false && Date.equals("") == false)
            {
                NameOut = Name;
                DateOut = Date;
                msg.setVisibility(View.VISIBLE);
            }
            else
            {
                msg.setText("Please complete both fields");
                msg.setVisibility(View.VISIBLE);
            }
        }
        return;

    }

12 个解决方案

#1


52  

SetOnClickListener (Android.View.view.OnClickListener) in View cannot be applied to (com.helloandroidstudio.MainActivity)

无法将视图中的SetOnClickListener (Android.View.view.OnClickListener)应用到(com.helloandroidstudio.MainActivity)

This means in other words (due to your current scenario) that your MainActivity need to implement OnClickListener:

这意味着(由于您当前的场景)您的主活动需要实现OnClickListener:

public class Main extends ActionBarActivity implements View.OnClickListener {
   // do your stuff
}

This:

这样的:

buttonname.setOnClickListener(this);

means that you want to assign listener for your Button "on this instance" -> this instance represents OnClickListener and for this reason your class have to implement that interface.

这意味着您想为您的按钮“在这个实例上”分配侦听器——>这个实例表示OnClickListener,因此您的类必须实现那个接口。

It's similar with anonymous listener class (that you can also use):

类似于匿名监听器类(也可以使用):

buttonname.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View view) {

   }
});

#2


7  

package com.mani.smsdetect;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

    //Declaration Button
    Button btnClickMe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Intialization Button

        btnClickMe = (Button) findViewById(R.id.btnClickMe);

        btnClickMe.setOnClickListener(MainActivity.this);
        //Here MainActivity.this is a Current Class Reference (context)
    }

    @Override
    public void onClick(View v) {

        //Your Logic
    }
}

#3


3  

Button button= (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
    // click handling code
   }
});

#4


2  

When you define an OnClickListener (or any listener) this way

当您以这种方式定义OnClickListener(或任何侦听器)时

btnClick.setOnClickListener(this);

you need to implement the OnClickListener in your Activity.

您需要在活动中实现OnClickListener。

public class MainActivity extends ActionBarActivity implements OnClickListener{

#5


2  

public class MainActivity extends AppCompatActivity implements View.OnClickListener

Whenever you use (this) on click events, your main activity has to implement ocClickListener. Android Studio does it for you, press alt+enter on the 'this' word.

每当您在单击事件上使用(this)时,您的主要活动必须实现ocClickListener。Android Studio为你做这个,按alt+enter键输入“this”。

#6


1  

//as I understand it, the "this" denotes the current view(focus) in the android program

//根据我的理解,“this”表示android程序的当前视图(焦点)

No, "this" will only work if your MainActivity referenced by this implements the View.OnClickListener, which is the parameter type for the setOnClickListener() method. It means that you should implement View.OnClickListener in MainActivity.

不,“this”只在此引用的主活动实现视图时才会工作。OnClickListener是setOnClickListener()方法的参数类型。这意味着您应该实现View。OnClickListener MainActivity。

#7


1  

package com.mani.helloworldapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    //Declaration
    TextView tvName;
    Button btnShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Empty Window
        super.onCreate(savedInstanceState);
        //Load XML File
        setContentView(R.layout.activity_main);

        //Intilization
        tvName = (TextView) findViewById(R.id.tvName);
        btnShow = (Button) findViewById(R.id.btnShow);

        btnShow.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        String name = tvName.getText().toString();
        Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
    }
}

#8


1  

Start your OnClickListener, but when you get to the first set up parenthesis, type new, then View, and press enter. Should look like this when you're done:

启动OnClickListener,但当您到达第一个设置括号时,输入new,然后是View,然后按enter。当你完成的时候应该像这样:

Button btn1 = (Button)findViewById(R.id.button1);

btn1.setOnClickListener(new View.OnClickListener() {            
    @Override
    public void onClick(View v) {
//your stuff here.
    }
});

#9


0  

public class MainActivity extends Activity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.submitButton);
        button.setOnClickListener(new MyClass());

    }

    public class MyClass implements View.OnClickListener {

        @Override
        public void onClick(View v) {

        }

    }
}

#10


0  

in Activity java class you would need a method first to find the view of the button as :

在Activity java类中,首先需要一个方法找到按钮的视图,如:

btnSum =(Button)findViewById(R.id.button);

after this set on click listener

在此设置之后单击listener

btnSum.setOnClickListener(new View.OnClickListener() {

and override onClick method for your functionality .I have found a fully working example here : http://javainhouse.blogspot.in/2016/01/button-example-android-studio.html

我在这里找到了一个完整的示例:http://javainhouse.blogspot.in/2016/01/button-example-android-studio.html

#11


0  

Your Activity must implement View.OnClickListener, like this:

您的活动必须实现View。OnClickListener,像这样:

public class MainActivity extends 
Activity implements View.OnClickListener{
// YOUR CODE
}

And inside MainActivity override the method onClick(), like this:

在MainActivity内部重写onClick()方法,如下所示:

@override
public void onClick (View view){
//here YOUR Action response to Click Button 
}

#12


-1  

Different ways to handle button event

处理按钮事件的不同方法

Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Button 1", 
     Toast.LENGTH_LONG).show();
        }
    });

[Check this article for more details about button event handlers]

[有关按钮事件处理程序的详细信息,请参阅本文]

#1


52  

SetOnClickListener (Android.View.view.OnClickListener) in View cannot be applied to (com.helloandroidstudio.MainActivity)

无法将视图中的SetOnClickListener (Android.View.view.OnClickListener)应用到(com.helloandroidstudio.MainActivity)

This means in other words (due to your current scenario) that your MainActivity need to implement OnClickListener:

这意味着(由于您当前的场景)您的主活动需要实现OnClickListener:

public class Main extends ActionBarActivity implements View.OnClickListener {
   // do your stuff
}

This:

这样的:

buttonname.setOnClickListener(this);

means that you want to assign listener for your Button "on this instance" -> this instance represents OnClickListener and for this reason your class have to implement that interface.

这意味着您想为您的按钮“在这个实例上”分配侦听器——>这个实例表示OnClickListener,因此您的类必须实现那个接口。

It's similar with anonymous listener class (that you can also use):

类似于匿名监听器类(也可以使用):

buttonname.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View view) {

   }
});

#2


7  

package com.mani.smsdetect;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

    //Declaration Button
    Button btnClickMe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Intialization Button

        btnClickMe = (Button) findViewById(R.id.btnClickMe);

        btnClickMe.setOnClickListener(MainActivity.this);
        //Here MainActivity.this is a Current Class Reference (context)
    }

    @Override
    public void onClick(View v) {

        //Your Logic
    }
}

#3


3  

Button button= (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
    // click handling code
   }
});

#4


2  

When you define an OnClickListener (or any listener) this way

当您以这种方式定义OnClickListener(或任何侦听器)时

btnClick.setOnClickListener(this);

you need to implement the OnClickListener in your Activity.

您需要在活动中实现OnClickListener。

public class MainActivity extends ActionBarActivity implements OnClickListener{

#5


2  

public class MainActivity extends AppCompatActivity implements View.OnClickListener

Whenever you use (this) on click events, your main activity has to implement ocClickListener. Android Studio does it for you, press alt+enter on the 'this' word.

每当您在单击事件上使用(this)时,您的主要活动必须实现ocClickListener。Android Studio为你做这个,按alt+enter键输入“this”。

#6


1  

//as I understand it, the "this" denotes the current view(focus) in the android program

//根据我的理解,“this”表示android程序的当前视图(焦点)

No, "this" will only work if your MainActivity referenced by this implements the View.OnClickListener, which is the parameter type for the setOnClickListener() method. It means that you should implement View.OnClickListener in MainActivity.

不,“this”只在此引用的主活动实现视图时才会工作。OnClickListener是setOnClickListener()方法的参数类型。这意味着您应该实现View。OnClickListener MainActivity。

#7


1  

package com.mani.helloworldapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    //Declaration
    TextView tvName;
    Button btnShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Empty Window
        super.onCreate(savedInstanceState);
        //Load XML File
        setContentView(R.layout.activity_main);

        //Intilization
        tvName = (TextView) findViewById(R.id.tvName);
        btnShow = (Button) findViewById(R.id.btnShow);

        btnShow.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        String name = tvName.getText().toString();
        Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
    }
}

#8


1  

Start your OnClickListener, but when you get to the first set up parenthesis, type new, then View, and press enter. Should look like this when you're done:

启动OnClickListener,但当您到达第一个设置括号时,输入new,然后是View,然后按enter。当你完成的时候应该像这样:

Button btn1 = (Button)findViewById(R.id.button1);

btn1.setOnClickListener(new View.OnClickListener() {            
    @Override
    public void onClick(View v) {
//your stuff here.
    }
});

#9


0  

public class MainActivity extends Activity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.submitButton);
        button.setOnClickListener(new MyClass());

    }

    public class MyClass implements View.OnClickListener {

        @Override
        public void onClick(View v) {

        }

    }
}

#10


0  

in Activity java class you would need a method first to find the view of the button as :

在Activity java类中,首先需要一个方法找到按钮的视图,如:

btnSum =(Button)findViewById(R.id.button);

after this set on click listener

在此设置之后单击listener

btnSum.setOnClickListener(new View.OnClickListener() {

and override onClick method for your functionality .I have found a fully working example here : http://javainhouse.blogspot.in/2016/01/button-example-android-studio.html

我在这里找到了一个完整的示例:http://javainhouse.blogspot.in/2016/01/button-example-android-studio.html

#11


0  

Your Activity must implement View.OnClickListener, like this:

您的活动必须实现View。OnClickListener,像这样:

public class MainActivity extends 
Activity implements View.OnClickListener{
// YOUR CODE
}

And inside MainActivity override the method onClick(), like this:

在MainActivity内部重写onClick()方法,如下所示:

@override
public void onClick (View view){
//here YOUR Action response to Click Button 
}

#12


-1  

Different ways to handle button event

处理按钮事件的不同方法

Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Button 1", 
     Toast.LENGTH_LONG).show();
        }
    });

[Check this article for more details about button event handlers]

[有关按钮事件处理程序的详细信息,请参阅本文]