Android Studio调试按钮按下 - 事件未触发

我使用设计模式屏幕设计创建了这个屏幕。

http://img2.mukewang.com/63510fb00001983202780490.jpg

这个问题基于 XML 的底部,带有 btnSave 按钮。


在我的 Java 类中,我试图创建一个onClickListener类似的


import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;


public class ftpDetails extends AppCompatActivity{

private TextView txtServer;

private TextView txtFolder;

private TextView txtUsername;

private TextView txtPassword;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_ftp_details);

    setTitle(R.string.ftpTitle);


    txtServer = (TextView) findViewById(R.id.txtServer);

    txtFolder = (TextView) findViewById(R.id.txtFolder);

    txtUsername = (TextView) findViewById(R.id.txtUsername);

    txtPassword = (TextView) findViewById(R.id.txtPassword);


findViewById(R.id.btnSave).setOnClickListener(null);


    // Check that all text boxes have a value in them

    if (txtServer.getText().length() == 0)

    {

        // MESSAGE BOX

        AlertDialog.Builder msg = new AlertDialog.Builder(this);

        msg.setTitle("Enter Server");

        msg.setMessage("Please enter a server address.");

        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                // Leave this blank, this will mean nothing happens, the msg just disappears

            }

        });

    }


但是,当我将应用程序 (Shift + F9) 调试到我连接的设备 (OnePlus 6T) 时,没有一个断点被命中。此外,当我按下按钮并且文本框为空白时,不会显示消息警报。


我做错了什么吗?我尝试使用 XMLonClick:属性,但结果相同,并且在设计模式下设置了相同的属性,但同样没有任何变化。


跃然一笑
浏览 290回答 4
4回答

GCT1015

你为什么使用这样的点击监听器:findViewById(R.id.btnSave).setOnClickListener(null);改为这样做:findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {               //put all your textView logic here            }        });

白衣非少年

更改此行:findViewById(R.id.btnSave).setOnClickListener(null);进入findViewById(R.id.btnSave).setOnClickListener(btnSaveListener);onCreate并从方法中删除以下行。然后创建btnSaveListener如下:private View.OnClickListener btnSaveListener =new View.OnClickListener() {   @Override   public void onClick(View v) {      // here goes all the code belove the line you change in the method `onCreate`   }};

慕盖茨4494581

最后,这不是我如何设置听众的问题。将活动调用从我的MainActivity启动类更改为StartActivity意味着OnCreate代码被正确调用。

慕容森

你的按钮点击监听器实现是错误的,试试这样: findViewById(R.id.action_ask).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                // Check that all text boxes have a value in them                if (txtServer.getText().length() == 0)                {                    // MESSAGE BOX                    AlertDialog.Builder msg = new AlertDialog.Builder(this);                    msg.setTitle("Enter Server");                    msg.setMessage("Please enter a server address.");                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            // Leave this blank, this will mean nothing happens, the msg just disappears                        }                    });                }                if (txtFolder.getText().length() == 0)                {                    // MESSAGE BOX                    AlertDialog.Builder msg = new AlertDialog.Builder(this);                    msg.setTitle("Enter Folder");                    msg.setMessage("Please enter a folder to use.");                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            // Leave this blank, this will mean nothing happens, the msg just disappears                        }                    });                }                if (txtUsername.getText().length() == 0)                {                    // MESSAGE BOX                    AlertDialog.Builder msg = new AlertDialog.Builder(this);                    msg.setTitle("Enter Username");                    msg.setMessage("Please enter your username.");                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            // Leave this blank, this will mean nothing happens, the msg just disappears                        }                    });                }                if (txtPassword.getText().length() == 0)                {                    // MESSAGE BOX                    AlertDialog.Builder msg = new AlertDialog.Builder(this);                    msg.setTitle("Enter Server");                    msg.setMessage("Please enter a your password.");                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            // Leave this blank, this will mean nothing happens, the msg just disappears                        }                    });                }            }        });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java