所以我做了一些研究,并在通过代码将按钮定义为对象后
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;
}
答案 0 :(得分:60)
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) {
}
});
答案 1 :(得分:7)
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
}
}
答案 2 :(得分:4)
Button button= (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// click handling code
}
});
答案 3 :(得分:2)
以这种方式定义OnClickListener
(或任何侦听器)时
btnClick.setOnClickListener(this);
您需要implement
OnClickListener
Activity
。
public class MainActivity extends ActionBarActivity implements OnClickListener{
答案 4 :(得分:2)
public class MainActivity extends AppCompatActivity implements View.OnClickListener
每当您对点击事件使用(this)时,您的主要活动必须实现ocClickListener。 Android Studio为您完成此操作,按下alt +输入'这个'字。
答案 5 :(得分:2)
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();
}
}
答案 6 :(得分:2)
启动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.
}
});
答案 7 :(得分:1)
//据我所知,“this”表示android程序中的当前视图(焦点)
不,“此”仅在MainActivity
引用的this
实现View.OnClickListener
时才有效,setOnClickListener()
是View.OnClickListener
方法的参数类型。这意味着您应该在MainActivity
中实施{{1}}。
答案 8 :(得分:1)
您可以简单地在按钮上设置 onClickListener 或使用 Lambda 表达式的任何内容,如下所示
private Button buttonname;
buttonname = (Button)findViewById(R.id.buttonnameinandroid);
buttonname.setOnClickListener(v ->
{
//Your Listener Code Here
});
答案 9 :(得分:0)
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) {
}
}
}
答案 10 :(得分:0)
在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
答案 11 :(得分:0)
您的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
}
答案 12 :(得分:0)
您什么也不需要做,只需在new View.OnClickListener()
中输入btnClick.setOnClickListener()
。我的意思是你必须输入
btnClick.setOnClickListener(new View.onClickListener
括号将保持打开状态,只需按Enter即可创建一个这样的方法
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
这仅意味着您正在创建OnClickListener的新视图并传递视图作为参数。现在,您可以使用该onClick(View v)方法执行您应做的事情。
答案 13 :(得分:-1)
处理按钮事件的不同方法
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]
答案 14 :(得分:-2)
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());
}
}