片段中的setOnKeyListener没有侦听

时间:2013-09-15 13:40:04

标签: android

这段代码中是否有我遗漏的东西? 是的,我知道这主要是代码,但恕我直言,我认为很清楚。 Stil似乎需要更多不必要的文本。

    @Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState)
{
    if(container == null) { return null; }
    super.onCreateView(inflater, container, savedInstanceState);
        final EditText editText = (EditText) outsideLayout.findViewById(R.id.prefPlayerName);
        editText.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT); 
        editText.setImeOptions(EditorInfo.IME_ACTION_GO);

        editText.setOnKeyListener(new EditText.OnKeyListener()
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                // If the event is a key-down event on the "enter" key
                if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
                {
                    Log.i(TAG, "PrefDetailsFragment enter key pressed");
                    return true;
                }
                return false;
            }
        });
    return outsideLayout;
}   // onCreateView()

1 个答案:

答案 0 :(得分:-1)

尝试在editText之外移动onCreateView并在onResume中设置监听器。您可能还想删除对super的调用,并在容器为null时退出。

@Override
protected void onResume() {
    super.onResume();
    if ( editText != null && ! editTexthasOnClickListeners() ) {
        editText.setOnKeyListener( ... );   // Add on click listener here as well
    }
}