【问题标题】:How to implement onClick in my activity如何在我的活动中实现 onClick
【发布时间】:2016-03-11 08:52:58
【问题描述】:

我在数据库中有密码(字符串)列表。 在我的活动中,我放置了 EditText(用户将在那里写下他/她的密码)和一个按钮。 我如何为将检查密码是否在系统中(在列表中)的按钮实现 onClick?

【问题讨论】:

    标签: java android android-layout android-activity android-studio


    【解决方案1】:

    在您的活动中定义一个名为 onClick() 的方法 - 单击按钮时将调用该方法(如您在 XML 中指定的那样)。然后,您可以检索 EditText 文本,并根据数据库中的密码检查它。

    public void onClick(View v) {
        EditText myEditText = (EditText) findViewById(R.id.password);
        CharSequence enteredPassword = myEditText.getText();
        // TODO check if input matches a string in the DB
    }
    

    【讨论】:

      【解决方案2】:

      对您希望用户点击的控件实现 OnClickListener:

       Button mLoginButton = (Button) findViewById(R.id.login);
          mLoginButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View view) {
                  Login();
              }
          });
      

      创建监听事件“登录”:

        private void attemptLogin() {
      
        //check login is valid
      
       }
      

      【讨论】:

        【解决方案3】:

        将此添加到按钮 btn.addActionListener( this ); 然后这样做 public void actionPerformed( ActionEvent event ) { if( event.getSource() == btn ) { } }

        【讨论】:

          【解决方案4】:
              You can do like this:
          
               Button loginButton = (Button) findViewById(R.id.login);
                  loginButton .setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          String pwd = password.getText().toString();
                          checkLogin(pwd)
                      }
                  })
          
              private void checkLogin(String password) {
          
               //db is your db instance.this is dummy query.You may have a user name editText as well
              Cursor c = db.rawQuery("SELECT * FROM tbl1 WHERE TRIM(password) = '"+password.trim()+"'", null);
              //check if cursor returns data
                 if (!(c .moveToFirst()) || c .getCount() ==0){
                   //cursor is empty
              }
              else
              {
          `//cursor is not empty`    
               }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-06-15
            • 1970-01-01
            • 2018-03-27
            相关资源
            最近更新 更多