0

我有一個數據庫中的密碼列表(字符串)。 在我的活動中,我把EditText(用戶將在那裏寫他/她的密碼)和一個按鈕。 我如何實現onClick按鈕來檢查密碼是否在系統中(在列表中)? in the image you can see part of the xml code如何在我的活動中實現onClick

回答

0

在您的活動中定義一個名爲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 
} 
0

實現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 

} 
0

這個添加到按鈕 btn.addActionListener(this); 然後這樣做 public void actionPerformed(ActionEvent event) { if(event.getSource() == btn) { } }

0
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`  
    } 
    } 
相關問題