当前位置: 首页 > 面试题库 >

如何在Android Studio中添加按钮单击事件

沈凡
2023-03-14
问题内容

因此,我进行了一些研究,并在代码中将按钮定义为对象之后

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

这是我的问题

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

那你的OnClick()活动…

问题:

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

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

我不知道为什么?

这是.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.html" target="_blank">setVisibility(View.VISIBLE);
            }
            else
            {
                msg.setText("Please complete both fields");
                msg.setVisibility(View.VISIBLE);
            }
        }
        return;

    }

问题答案:

View中的SetOnClickListener(Android.View.view.OnClickListener)无法应用于(com.helloandroidstudio.MainActivity)

换句话说(由于您当前的情况),这意味着MainActivity需要实现 OnClickListener

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

这个:

buttonname.setOnClickListener(this);

意味着您要 “在此实例上” 为Button分配侦听器,->该实例表示 OnClickListener ,因此,您的类必须实现该接口。

与匿名侦听器类类似(您也可以使用):

buttonname.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View view) {

   }
});


 类似资料:
  • 下面是.java文件中的代码

  • 有没有人能告诉我如何在按钮的onclick上添加监听器,但它已经在按钮onclick上有功能了,以及如何在按钮上勾选onclick功能,这里我提供了jsfiddel对此请提前帮助我,谢谢回复http://jsfidle.net/jwub4/15/

  • 编辑:底部的解决方案 这是一个跳棋游戏。单击一个按钮后,它等待单击第二个按钮与之交换。然而,有时你可能不想移动那个按钮,但一旦你点击了它,就没有回头路了,因为我无法禁用它。 在这里的其他帖子中,我看到人们使用 这只是使它在第一次单击后不可见。 这什么都干不了。 这也没什么用。编辑:所有这些方法都用true和false进行了尝试。 私有无效交换(){ 但你也需要 这样它就重新启用了它,或者其他什么,

  • 问题内容: 我只是试图在我的注释点添加一个细节按钮而被卡住,不幸的是,我不知道该怎么做。有人可以帮我吗? 下图显示了我想要实现的目标。谢谢! MapKitViewController: 问题答案: 您做对了,您只需要实现这些方法即可添加按钮以及标题和副标题 iOS 8和Xcode 6 查看我的输出。

  • 现在我们想去检测用户是有将按键按下了。 在Contiki中,将按键当做是一个传感器。我们将会使用库core/dev/button-sensor.h。 RE-Mote平台实现了额外的按键功能,比如长按检测,可用于将来扩展由按键触发的事件。文件platform/zoul/dev/button-sensor.c中有更详细的介绍,文件examples/zolertia/zoul/zoul-demo.c中有

  • 我想在单击JFrame标题栏的红色close按钮时调用一个方法。 我如何捕捉那个事件?