0

嗯..也许是一个有点愚蠢的问题,但我自己不明白......

    package com.example.wettkampftimerbt;
    import android.content.Intent;
    import android.inputmethodservice.Keyboard.Key;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class ServMPW extends Activity_ActivityGroup implements OnClickListener{

        Button enter, cancel;
        EditText eingabe, eingabe2, masterpw;
        String pwx, eingabex, pass, adminacchint1x, adminacchint2x;
    private final String PREFS_NAME = "prefs";  
    private final String PREFS_PW = "pw";
    private final String PREFS_lang = "lang";
    private final String TITLE5 = "title5";
    private final String ADMINACCHINT1 = "adminacchint1";
    private final String ADMINACCHINT2 = "adminacchint2";
        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
     setContentView(R.layout.servmpw);   
     //((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
     String title5x=(getSharedPreferences(PREFS_lang, MODE_PRIVATE).getString(TITLE5, "Administrationszugang"));
    this.setTitle(title5x);
     adminacchint1x=(getSharedPreferences(PREFS_lang, MODE_PRIVATE).getString(ADMINACCHINT1, "Passwort"));
     adminacchint2x=(getSharedPreferences(PREFS_lang, MODE_PRIVATE).getString(ADMINACCHINT2, "falsches Passwort"));

            enter=(Button)findViewById(R.id.enter);
            cancel=(Button)findViewById(R.id.cancel);
            eingabe=(EditText)findViewById(R.id.eingabe);
            masterpw=(EditText)findViewById(R.id.masterpw);
            eingabe.setHint(adminacchint1x);
            eingabe2=(EditText)findViewById(R.id.eingabe2);
            enter.setOnClickListener(this);
            cancel.setOnClickListener(this);

    }
            public void onClick (View v){

             if (v==cancel){
                    finish();}
            else if (v == enter){
                entermeth();
            }   

                    else {
                eingabe.setText("");
                eingabe.setHint(adminacchint2x);}}



public void OnKey (View v, int keyCode, KeyEvent event) 
{
    if (keyCode==66)
        entermeth();
}
public void entermeth(){

    eingabe2.setText(getSharedPreferences(PREFS_NAME, MODE_PRIVATE).getString(PREFS_PW, "bbb"));  
    pwx = eingabe2.getText().toString();
    Log.e("pw", PREFS_PW + "." + pwx);
    eingabex = eingabe.getText().toString(); 
    masterpw.setText("aaa");
    String masterpwx = masterpw.getText().toString();
    Log.e("pw:", masterpwx);
    if (eingabex.equals(pwx)||(eingabex.equals(masterpwx))){
        Intent admin = new Intent (ServMPW.this, Admin.class);
        ServMPW.this.finish();
        startActivity(admin);}


}
}

如果按下软键盘上的回车按钮,我想点击我自己的回车按钮。但它不起作用......我错过了什么?

4

2 回答 2

2

在你的EditText你可以绑定OnEditorActionListener. 像这样的东西

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
       if (actionId == EditorInfo.IME_ACTION_DONE) {
           //do here your stuff here
           return true;
       }
       return false;
   }
});
于 2013-02-07T12:24:00.293 回答
1

什么都没有调用该OnKey方法。这就是你错过的。此外,强制点击也不是一个好的编程习惯。相反,您应该创建一个方法private doEnterActions(){ },并在输入键和输入按钮上调用该方法。

编辑:

就像你 setClickListener 到你的按钮(由活动实现)你必须使活动实现,并设置为关键事件的侦听器。这是使用完成的enter.setOnKeyListener()

于 2013-02-07T12:17:07.103 回答