为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

跃然一笑 2022-10-20 17:06:38
我使用设计模式屏幕设计创建了这个屏幕。这个问题基于 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;@Overrideprotected 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:属性,但结果相同,并且在设计模式下设置了相同的属性,但同样没有任何变化。
查看完整描述

4 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

你为什么使用这样的点击监听器: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

            }

        });


查看完整回答
反对 回复 2022-10-20
?
白衣非少年

TA贡献1155条经验 获得超0个赞

更改此行:


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`

   }

};


查看完整回答
反对 回复 2022-10-20
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

最后,这不是我如何设置听众的问题。

将活动调用从我的MainActivity启动类更改为StartActivity意味着OnCreate代码被正确调用。



查看完整回答
反对 回复 2022-10-20
?
慕容森

TA贡献1853条经验 获得超18个赞

你的按钮点击监听器实现是错误的,试试这样:


 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

                        }

                    });

                }

            }

        });



查看完整回答
反对 回复 2022-10-20
  • 4 回答
  • 0 关注
  • 289 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号