如何添加按钮单击事件中的android工作室 [英] how to add button click event in android studio
问题描述
所以,我做了一些调查,并确定你按键为对象由code后
专用按钮buttonname;
buttonname =(按钮)findViewById(R.id.buttonnameinandroid);
下面是我的问题。
buttonname.setOnClickListener(本); //我的理解中,**这**表示在Android程序中的当前`视图(重点)`
那么你的的OnClick()
事件...
问题:
当我键入本,它说:
setOnClickListener(Android.View.view.OnClickListener)的视图不能适用于(com.helloandroidstudio.MainActivity)
我不知道为什么?
下面是从.java文件的code
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
公共类MainActivity扩展ActionBarActivity {
私人按钮btnClick;
私人的EditText名称,日期;
私人TextView的味精,NameOut,DateOut;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
btnClick =(按钮)findViewById(R.id.button);
btnClick.setOnClickListener(本);
名称=(EditText上)findViewById(R.id.textenter);
日期=(EditText上)findViewById(R.id.editText);
味精=(TextView中)findViewById(R.id.txtviewOut);
NameOut =(TextView中)findViewById(R.id.txtoutName);
DateOut =(TextView中)findViewById(R.id.txtOutDate);
如果(savedInstanceState == NULL){
getSupportFragmentManager()的BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())
。承诺();
}
}
公共无效的onClick(视图v)
{
如果(V == btnClick)
{
如果(Name.equals()==假放;&安培; Date.equals()==假)
{
NameOut =名称;
DateOut =日期;
msg.setVisibility(View.VISIBLE);
}
其他
{
msg.setText(请填写这两个字段);
msg.setVisibility(View.VISIBLE);
}
}
返回;
}
SetOnClickListener(Android.View.view.OnClickListener)的视图不能 被应用到(com.helloandroidstudio.MainActivity)
这意味着,换言之(由于您目前的情况),您的MainActivity需要实施 OnClickListener
公共类主要扩展ActionBarActivity实现View.OnClickListener {
//做你的东西
}
本:
buttonname.setOnClickListener(本);
表示要为其指定监听器为您的按钮的在这种情况下的 - >
这种情况下再presents <强> OnClickListener 基于这个原因,你的类必须实现该接口。
这是匿名监听类相似(您也可以使用):
buttonname.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图查看){
}
});
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...
Problem:
When I type in the "this", it says:
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
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;
}
SetOnClickListener (Android.View.view.OnClickListener) in View cannot be applied to (com.helloandroidstudio.MainActivity)
This means in other words (due to your current scenario) that your MainActivity need to implement 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.
It's similar with anonymous listener class (that you can also use):
buttonname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
这篇关于如何添加按钮单击事件中的android工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!