点击监听器无法正常工作

时间:2018-01-13 16:05:36

标签: android

嗨,我是Android学习的新手,我正在练习它。我想在我的按钮上实现点击监听器,但它给了我错误。甚至我导入了android.view.View.OnClickListener;和其他所有图书馆。 请查看我的代码检查我的错误在哪里

  

无法解析onClickelistner上的符号。

package com.example.nexus.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    Button button;
    TextView loginresulttext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.loginbutton);
        loginresulttext = (TextView) findViewById(R.id.loginresult);
    }


    button.OnClickListener(new OnClickListener()
    {

        public void onClick(View v)
        {

            Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();

        }
    });
}

4 个答案:

答案 0 :(得分:0)

试试这个并将其放入onCreate方法

DECLARE
    table_exist INT;

BEGIN
    SELECT Count(*)
    INTO   table_exist
    FROM   dba_tables
    WHERE  owner = 'SCHEMA_NAME' 
    AND table_name = 'EMPLOYEE_TABLE';

    IF table_exist = 1 THEN
      EXECUTE IMMEDIATE 'drop table EMPLOYEE_TABLE';
    END IF;
END;  

答案 1 :(得分:0)

结合R2R和Toan Tran的答案将为您提供最佳解决方案:

public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.loginbutton);
    loginresulttext = (TextView) findViewById(R.id.loginresult);
    button.setOnClickListener(new OnClickListener()
    {

        public void onClick(View v)
        {

            Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();

        }
    });
}

答案 2 :(得分:0)

这应该有用。

Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
  public void onClick(View view) {
    // TODO Auto-generated method stub
  }
 });

答案 3 :(得分:-1)

尝试按钮点击 Oncreate()

public class MainActivity extends AppCompatActivity {
    Button button;
    TextView loginresulttext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.loginbutton);
        loginresulttext = (TextView) findViewById(R.id.loginresult);
         button.setOnClickListener(new OnClickListener(){
        public void onClick(View v)
        {
            Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
        }
        });
    }
}