Java 按钮单击不起作用:按钮不起任何作用

Java 按钮单击不起作用:按钮不起任何作用,java,android,onclicklistener,Java,Android,Onclicklistener,我正在使用下面的课程,在过去的24小时里我一直在尝试,我不明白为什么它不起作用。当我按下按钮时,它什么也不做 我创建它是为了验证使用登录信息 我的班级 public class login extends AppCompatActivity { Button button11; EditText usernameField, passwordField; @Override protected void onCreate(Bundle savedInstanceState) { su

我正在使用下面的课程,在过去的24小时里我一直在尝试,我不明白为什么它不起作用。当我按下按钮时,它什么也不做

我创建它是为了验证使用登录信息

我的班级

public class login extends AppCompatActivity  {
Button button11;
EditText usernameField, passwordField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    usernameField = (EditText) findViewById(R.id.user);
    passwordField = (EditText) findViewById(R.id.password);


    Button clickButton = (Button) findViewById(R.id.loginButton);
    clickButton.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"it 
            clicked",Toast.LENGTH_LONG);
            TextView mt=(TextView) findViewById(R.id.messy);

            mt.setText("Please Wait");
        }
    });


}}

我在

上添加了我的XML文件。您正在创建Toast,但不调用它来显示其内容。请尝试代码:

 @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"it 
            clicked",Toast.LENGTH_LONG).show();
            TextView mt=(TextView) findViewById(R.id.messy);

            mt.setText("Please Wait");
        }

您忘记使用show()

请尝试此解决方案

 Toast.makeText(login.this,"it 
        clicked",Toast.LENGTH_LONG).show;
而不是现有的getApplicationContext()

试试这个


当事情变得更糟的时候。。您可以查看基本日志。试试这个~

public class login extends AppCompatActivity  {

Button button11;
EditText usernameField, passwordField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    usernameField = (EditText) findViewById(R.id.user);
    passwordField = (EditText) findViewById(R.id.password);


    Button clickButton = (Button) findViewById(R.id.loginButton);

    clickButton.setText("Button Found");

    clickButton.setOnClickListener( new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        clickButton.setText("Button Clicked");
        }
    });
}}

还有另一种编写按钮单击代码的方法

public class login extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Button firstButton = (Button) findViewById(R.id.loginButton);
        firstButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton: {
                Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();

                TextView textView = (TextView) findViewById(R.id.messy);
                textView.setText("Your text here");
                break;
            }
            default: {
                Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }
}

我希望这能解决您的问题。

将layour layout.xml放在那里,问题就在那里,如果getApplicationContext尝试使用“this”关键字。将Toast调用更改为普通的:Toast.makeText(v.getContext(),“clicked”,Toast.LENGTH_LONG)。show();谢谢你的支持。我解决了这个问题
public class login extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Button firstButton = (Button) findViewById(R.id.loginButton);
        firstButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton: {
                Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();

                TextView textView = (TextView) findViewById(R.id.messy);
                textView.setText("Your text here");
                break;
            }
            default: {
                Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }
}