如何在android studio中添加按钮点击事件
Posted
技术标签:
【中文标题】如何在android studio中添加按钮点击事件【英文标题】:how to add button click event in android studio 【发布时间】:2013-12-08 00:49:06 【问题描述】:所以我做了一些研究,在通过代码将你的按钮定义为一个对象之后
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;
【问题讨论】:
【参考方案1】: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);
意味着你想为你的按钮分配监听器“在这个实例上”->
这个实例代表OnClickListener,因此你的类必须实现那个接口.
类似匿名监听类(你也可以使用):
buttonname.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
);
【讨论】:
【参考方案2】:Button button= (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
// click handling code
);
【讨论】:
这个答案绝对没有错,但总的来说,如果答案包含对它们起作用的为什么的一些描述,而不是只是做什么来解决这个问题。欢迎加入社区!【参考方案3】: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
【讨论】:
【参考方案4】: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();
【讨论】:
你能对你的回答做任何描述吗?【参考方案5】:当您以这种方式定义OnClickListener
(或任何侦听器)时
btnClick.setOnClickListener(this);
您需要在您的Activity
中implement
OnClickListener
。
public class MainActivity extends ActionBarActivity implements OnClickListener
【讨论】:
【参考方案6】:public class MainActivity extends AppCompatActivity implements View.OnClickListener
每当您在点击事件上使用 (this) 时,您的主要活动都必须实现 ocClickListener。 Android Studio 会为您完成,在“this”字词上按 alt+enter。
【讨论】:
【参考方案7】:启动您的 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.
);
【讨论】:
【参考方案8】:您可以简单地在按钮上设置 onClickListener 或使用看起来像这样的 Lambda 表达式的任何东西
private Button buttonname;
buttonname = (Button)findViewById(R.id.buttonnameinandroid);
buttonname.setOnClickListener(v ->
//Your Listener Code Here
);
【讨论】:
【参考方案9】://据我了解,“this”表示android程序中的当前视图(焦点)
不,“this”仅在 this
引用的 MainActivity
实现 View.OnClickListener
(setOnClickListener()
方法的参数类型)时才有效。这意味着你应该在MainActivity
中实现View.OnClickListener
。
【讨论】:
【参考方案10】: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)
【讨论】:
【参考方案11】:在 Activity java 类中,您首先需要一个方法来找到按钮的视图:
btnSum =(Button)findViewById(R.id.button);
在点击监听器上设置后
btnSum.setOnClickListener(new View.OnClickListener()
并为您的功能覆盖 onClick 方法。我在这里找到了一个完整的示例:http://javainhouse.blogspot.in/2016/01/button-example-android-studio.html
【讨论】:
【参考方案12】:你的Activity
必须实现View.OnClickListener
,像这样:
public class MainActivity extends
Activity implements View.OnClickListener
// YOUR CODE
而在MainActivity
内部重写onClick()
方法,像这样:
@override
public void onClick (View view)
//here YOUR Action response to Click Button
【讨论】:
【参考方案13】:您无需执行任何操作,只需在您的btnClick.setOnClickListener()
中输入new View.OnClickListener()
。我的意思是你必须输入
btnClick.setOnClickListener(new View.onClickListener
括号将保持打开状态,只需按 Enter 即可创建这样的方法,
btnClick.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
);
这只是意味着您正在创建 OnClickListener 的新视图并将视图作为参数传递。现在你可以在 onClick(View v) 方法中做任何你应该做的事情。
【讨论】:
【参考方案14】:处理按钮事件的不同方式
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]
【讨论】:
【参考方案15】:public class EditProfile extends AppCompatActivity
Button searchBtn;
EditText userName_editText;
EditText password_editText;
EditText dob_editText;
RadioGroup genderRadioGroup;
RadioButton genderRadioBtn;
Button editBtn;
Button deleteBtn;
Intent intent;
DBHandler dbHandler;
public static final String USERID_EDITPROFILE = "userID";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
searchBtn = (Button)findViewById(R.id.editprof_searchbtn);
userName_editText = (EditText)findViewById(R.id.editprof_userName);
password_editText = (EditText)findViewById(R.id.editprof_password);
dob_editText = (EditText)findViewById(R.id.editprof_dob);
genderRadioGroup = (RadioGroup)findViewById(R.id.editprof_radiogroup);
editBtn = (Button)findViewById(R.id.editprof_editbtn);
deleteBtn = (Button)findViewById(R.id.editprof_deletebtn);
intent = getIntent();
dbHandler = new DBHandler(EditProfile.this);
setUserDetails();
deleteBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String username = userName_editText.getText().toString();
if(username == null)
Toast.makeText(EditProfile.this,"Please enter username to delete your profile",Toast.LENGTH_SHORT).show();
else
UserProfile.Users users = dbHandler.readAllInfor(username);
if(users == null)
Toast.makeText(EditProfile.this,"No profile found from this username, please enter valid username",Toast.LENGTH_SHORT).show();
else
dbHandler.deleteInfo(username);
Intent redirectintent_home = new Intent("com.modelpaper.mad.it17121002.Home");
startActivity(redirectintent_home);
);
editBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String userID_String = intent.getStringExtra(Home.USERID);
if(userID_String == null)
Toast.makeText(EditProfile.this,"Error!!",Toast.LENGTH_SHORT).show();
Intent redirectintent_home = new Intent(getApplicationContext(),Home.class);
startActivity(redirectintent_home);
int userID = Integer.parseInt(userID_String);
String username = userName_editText.getText().toString();
String password = password_editText.getText().toString();
String dob = dob_editText.getText().toString();
int selectedGender = genderRadioGroup.getCheckedRadioButtonId();
genderRadioBtn = (RadioButton)findViewById(selectedGender);
String gender = genderRadioBtn.getText().toString();
UserProfile.Users users = UserProfile.getProfile().getUser();
users.setUsername(username);
users.setPassword(password);
users.setDob(dob);
users.setGender(gender);
users.setId(userID);
dbHandler.updateInfor(users);
Toast.makeText(EditProfile.this,"Updated Successfully",Toast.LENGTH_SHORT).show();
Intent redirectintent_home = new Intent(getApplicationContext(),Home.class);
startActivity(redirectintent_home);
);
searchBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
String username = userName_editText.getText().toString();
if (username == null)
Toast.makeText(EditProfile.this,"Please enter a username",Toast.LENGTH_SHORT).show();
else
UserProfile.Users users_search = dbHandler.readAllInfor(username);
if(users_search == null)
Toast.makeText(EditProfile.this,"Please enter a valid username",Toast.LENGTH_SHORT).show();
else
userName_editText.setText(users_search.getUsername());
password_editText.setText(users_search.getPassword());
dob_editText.setText(users_search.getDob());
int id = users_search.getId();
Intent redirectintent = new Intent("com.modelpaper.mad.it17121002.EditProfile");
redirectintent.putExtra(USERID_EDITPROFILE,Integer.toString(id));
startActivity(redirectintent);
);
public void setUserDetails()
String userID_String = intent.getStringExtra(Home.USERID);
if(userID_String == null)
Toast.makeText(EditProfile.this,"Error!!",Toast.LENGTH_SHORT).show();
Intent redirectintent_home = new Intent("com.modelpaper.mad.it17121002.Home");
startActivity(redirectintent_home);
int userID = Integer.parseInt(userID_String);
UserProfile.Users users = dbHandler.readAllInfor(userID);
userName_editText.setText(users.getUsername());
password_editText.setText(users.getPassword());
dob_editText.setText(users.getDob());
【讨论】:
以上是关于如何在android studio中添加按钮点击事件的主要内容,如果未能解决你的问题,请参考以下文章
[Android Studio 权威教程]Android Studio 三种添加插件的方式