2011-02-02 4 views
9

키 이벤트에 애니메이션을 구현하려고합니다. 동일한 코드가 OnClickListener에서 잘 작동하지만 OnKeyListener에서는 아무 것도 일어나지 않습니다. Logcat에도 오류 메시지가 표시되지 않습니다.OnKeyListener가 작동하지 않습니다.

이 문제를 해결하는 데 도움을주십시오. 아래는 제 코드입니다. 내가 틀린 곳을 말해줘.

package com.example.SliderImage; 

import android.app.Activity; 

import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnKeyListener; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.ViewFlipper; 

public class SliderImage extends Activity { 
    ViewFlipper flipper; 
    String value; 
    int i; 

    int menuArray[] = {R.id.gtmain, R.id.gtmini, R.id.gtmore}; 

    protected void showPrevious() { 
      // TODO Auto-generated method stub 

     } 
     protected void setInAnimation(Animation inFromRightAnimation) { 
      // TODO Auto-generated method stub 

     } 
     protected void setOutAnimation(Animation outToLeftAnimation) { 
      // TODO Auto-generated method stub 

     } 
     protected void showNext() { 
      // TODO Auto-generated method stub 

     } 
     private Animation inFromRightAnimation() { 

      Animation inFromRight = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
      ); 
      inFromRight.setDuration(500); 
      inFromRight.setInterpolator(new AccelerateInterpolator()); 
      return inFromRight; 
      } 
     private Animation outToLeftAnimation() { 

      Animation outtoLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
      ); 
      outtoLeft.setDuration(500); 
      outtoLeft.setInterpolator(new AccelerateInterpolator()); 
      return outtoLeft; 
      } 



     private Animation inFromLeftAnimation() { 

      Animation inFromLeft = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
      ); 
      inFromLeft.setDuration(500); 
      inFromLeft.setInterpolator(new AccelerateInterpolator()); 
      return inFromLeft; 
      } 
     private Animation outToRightAnimation() { 

      Animation outtoRight = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
      ); 
      outtoRight.setDuration(500); 
      outtoRight.setInterpolator(new AccelerateInterpolator()); 
      return outtoRight; 
      } 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home); 

    flipper = (ViewFlipper) findViewById(R.id.flipper); 

    final Button miniButton = (Button) findViewById(R.id.gtmini); 
    final Button mainButton = (Button) findViewById(R.id.gtmain); 
    final Button moreButton = (Button) findViewById(R.id.gtmore); 

    miniButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      flipper.setInAnimation(inFromRightAnimation()); 
      flipper.setOutAnimation(outToLeftAnimation()); 
      flipper.showNext();  
     } 
    }); 

    mainButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      flipper.setInAnimation(inFromLeftAnimation()); 
      flipper.setOutAnimation(outToRightAnimation()); 
      //flipper.showPrevious(); 
      flipper.showNext(); 
     } 
    }); 
    moreButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      flipper.setInAnimation(inFromLeftAnimation()); 
      flipper.setOutAnimation(outToRightAnimation()); 
      // flipper.showPrevious(); 
      flipper.showNext(); 
     } 
    }); 

    mainButton.setOnKeyListener(new OnKeyListener() { 

     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      String a1=String.valueOf(keyCode); 
      Log.i("listvalaaaaaaaaaa",a1); 
      if (keyCode==66) { 
       //String value=mainButton.getText().toString(); 
       if(value.equalsIgnoreCase("GO TO MAIN")) 
       { 
        //startActivity(new Intent(Start.this,MainEpg.class)); 
       } 
       if(value.equalsIgnoreCase("GO TO MINI")) 
       { 
        //startActivity(new Intent(Start.this,MiniEpg.class)); 
       } 
      } 

      if (keyCode==22) { 

       Log.i("insideright","ghkhkjhk"); 
       flipper.setInAnimation(inFromRightAnimation()); 
       flipper.setOutAnimation(outToLeftAnimation()); 
       flipper.showNext();  

      } 
       int c = moreButton.getId(); 
       int b = mainButton.getId(); 
       int a = moreButton.getId(); 

       menuArray[0] = b; 
       menuArray[1] = c; 
       menuArray[2] = a; 

       moreButton.setId(menuArray[i]); 
       miniButton.setId(menuArray[++i]); 
       mainButton.setId(menuArray[++i]); 

       mainButton.requestFocus(); 
       i = 0; 
       //mainButton.requestFocus(); 
       mainButton.setSelected(true); 
       flipper.setInAnimation(inFromRightAnimation()); 
       flipper.setOutAnimation(outToLeftAnimation()); 
       flipper.showNext();  

      if (keyCode==21) { 

       int a11 = moreButton.getId(); 
       int b1 = mainButton.getId(); 
       int c1 = moreButton.getId(); 

       menuArray[0] = c1; 
       menuArray[1] = b1; 
       menuArray[2] = a11; 

       moreButton.setId(menuArray[i]); 
       miniButton.setId(menuArray[++i]); 
       mainButton.setId(menuArray[++i]); 

       mainButton.requestFocus(); 
       i = 0; 

       //mainButton.requestFocus(); 
       mainButton.setSelected(true); 
       /* flipper.setInAnimation(inFromRightAnimation()); 
       flipper.setOutAnimation(outToLeftAnimation()); 
       flipper.showPrevious();*/ 
      } 
      return true; 
      } 
     }); 


    } 



} 
+0

향후 * 문제를 재현하는 * 최소 예 *를 보여주십시오. – Qix

답변

0

나는 키 리스너에 대한 구현 방법은 다음과 같습니다 :

this.m_vwJokeEditText.setOnKeyListener(new OnKeyListener() { 

     @Override 
     public boolean onKey(View arg0, int arg1, KeyEvent arg2) { 
      if (arg2.getAction() == KeyEvent.ACTION_DOWN) { 
       if (arg1 == KeyEvent.KEYCODE_DPAD_CENTER 
         || arg1 == KeyEvent.KEYCODE_ENTER) { 

        // do something here 

        return true; 
       } 
      } 
      return false; 
     } 

    }); 
+0

키를 누르면 onKey가 호출됩니까? –

+0

예 -이 코드는 키 동작이 중단되고 눌려진 키가 dpad 또는 enter 일 때 무언가를 수행합니다. 질문 코드의 문제는 KeyEvent와 int Action이 섞여 있다는 것입니다. – eosgood

1

KeyCode들 사용하는 키보드에 따라 달라집니다. 동일한 키에 대해 서로 다른 키보드에서 다른 KeyCode을 얻을 수 있습니다. documentation에서

View.OnKeyListener는 하드웨어 키보드에만 유용합니다. 소프트웨어 입력 방법은이 청취자를 트리거 할 의무가 없습니다.

관련 문제