gpt4 book ai didi

android - OnKeyListener 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:12 26 4
gpt4 key购买 nike

我正在尝试在关键事件上实现动画。相同的代码在 OnClickListener 上运行良好,但在 OnKeyListener 上没有任何反应。甚至在 Logcat 上也没有错误消息。

请帮我解决这个问题。下面是我的代码。请告诉我哪里错了。

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;
}
});


}



}

最佳答案

KeyCodes 取决于您使用的键盘。这意味着对于同一个键,您可能会从不同的键盘获得不同的 KeyCode。请记住,来自 documentation ,

View.OnKeyListener is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

关于android - OnKeyListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4878718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com