2011-09-18 7 views
2

OnKey()メソッドのコードが実行されていない理由をお手伝いしますか?私はアンドロイドキーボードの "検索"ボタンを押してこれを起動しようとしています。私は修正案として、以下のコードのブロックを追加しようとしたが、それは助けにはならなかったOnKeyListener()が実行されていません

package com.onkeyexample1; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnKeyListener; 
import android.widget.EditText; 

public class OnKeyExample1Activity extends Activity { 
EditText editText; 
OnKeyListener onKeyListener; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    editText = (EditText) findViewById(R.id.editText1); 
    editText.setOnKeyListener(onKeyListener); 
    editText.addTextChangedListener(inputTextWatcher); 

onKeyListener = new OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
      System.out.println("Clicked"); 
     return true; 
       } 
    }; 
} 

:ここ

がメインのコードである。ここ

private TextWatcher inputTextWatcher = new TextWatcher() { 
     public void afterTextChanged(Editable s) { } 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) 
      { } 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // Log.d(TAG, s.charAt(count-1) + " character to send");;   
     } 
    }; 
} 

は私のレイアウトxmlです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<EditText android:imeOptions="actionSearch" android:layout_width="fill_parent" 
    android:id="@+id/editText1" android:layout_height="wrap_content"> 
    <requestFocus></requestFocus> 
</EditText> 
</LinearLayout> 

ご意見をいただければ幸いです!ここで

はテッドの提案の最初の実装です:

public class OnKeyExample1Activity extends Activity { 
EditText editText; 
OnKeyListener onKeyListener; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    editText = (EditText) findViewById(R.id.editText1); 
editText.setOnKeyListener(onKeyListener = new OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
      System.out.println("Clicked"); 
     return true; 
       } 
}); 
} 
} 

は、ここに私のコメントで述べたダブルトリガーの問題に対する修正です:

editText.setOnKeyListener(onKeyListener = new OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if (event.getAction() != KeyEvent.ACTION_DOWN) { 
      System.out.println("Argh"); 
      return false; 
     } 
     return true; 
    } 
}); 
} 

答えて

2

あなたがあなたの前にonKeyListenerに値を割り当てる必要がありますeditText.setOnKeyListener(onKeyListener);に電話してください。代入をメソッド呼び出しの前に移動するだけで済みます。

+0

Ted、私は今onKey()内のコードが2回実行されているように思えるあなたの提案に感謝します。これには何らかの理由が考えられますか? もう一度ご協力いただきありがとうございます。 – Ben

+0

Ted、ダブルトリガの問題を修正しました(上記の私のコードを参照)。あなたに助けてくれてありがとう! – Ben

関連する問題