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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20156733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:33:58  来源:igfitidea点击:

how to add button click event in android studio

javaandroidbuttonviewandroid-studio

提问by

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;

    }

采纳答案by Simon Dorociak

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

View中的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:

这意味着换句话说(由于您当前的情况)您的 MainActivity 需要实现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 OnClickListenerand 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) {

   }
});

回答by Egor

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

//据我所知,“this”表示android程序中的当前视图(焦点)

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

不,“this”仅在您MainActivity引用的方法this实现了时才有效View.OnClickListener,这是该setOnClickListener()方法的参数类型。这意味着您应该View.OnClickListenerMainActivity.

回答by codeMagic

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

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

btnClick.setOnClickListener(this);

you need to implementthe OnClickListenerin your Activity.

你需要implementOnClickListener你的Activity

public class MainActivity extends ActionBarActivity implements OnClickListener{

回答by Gagandeep

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 会为您完成,在“this”字样上按 alt+enter。

回答by MaxEcho

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) {

        }

    }
}

回答by Manikanta Veeram

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();
    }
}

回答by Gagan

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

在此设置后单击侦听器

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

并为您的功能覆盖 onClick 方法。我在这里找到了一个完整的示例:http: //javainhouse.blogspot.in/2016/01/button-example-android-studio.html

回答by Manikanta Reddy

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
    }
}

回答by Mohamed Zidan

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

回答by user7925882

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]

[查看此文章以了解有关按钮事件处理程序的更多详细信息]