失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android studio按钮点击事件 如何在Android Studio中添加按钮单击事件

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

时间:2020-09-25 16:01:29

相关推荐

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

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

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.setVisibility(View.VISIBLE);

}

else

{

msg.setText("Please complete both fields");

msg.setVisibility(View.VISIBLE);

}

}

return;

}

如果觉得《android studio按钮点击事件 如何在Android Studio中添加按钮单击事件》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。