gpt4 book ai didi

android - 不进入 Onkeylistener?无法弄清楚

转载 作者:行者123 更新时间:2023-11-29 22:14:01 32 4
gpt4 key购买 nike

   package woot.wat.wen;

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HmmActivity extends Activity implements OnKeyListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//RelativeLayout Relay=(RelativeLayout) getResources().getLayout(R.layout.main);
ViewGroup group = (ViewGroup) findViewById(R.id.relativeLayout1);
TextView tv=(TextView) findViewById(R.id.tv1);
MarginLayoutParams mapara = new MarginLayoutParams(tv.getLayoutParams());
mapara.setMargins(225, 260, 120, 120);


LayoutParams params=new RelativeLayout.LayoutParams(mapara);
tv.setLayoutParams(params);
group.setFocusable(true);
group.setOnKeyListener(this);

int children = group.getChildCount();
for (int i = 0; i < children; i++) {

View child = group.getChildAt(i);
child.setFocusable(true);
child.setOnKeyListener(this);


}



//tv.setOnKeyListener(this);


}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// back=true;
// do something on back.
Toast.makeText(this, "Back key pressed", Toast.LENGTH_SHORT);




System.out.println("WTF");


return true;
}



else

return super.onKeyDown(keyCode, event);
}


public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK /*&& event.getRepeatCount() == 0*/) {
// back=true;
// do something on back.
Log.d("backk", "goingoin backback");






return true;
}

else
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
//return false;
}






}

正如您在代码中看到的那样,当按下后退键时,我正在尝试执行某些操作,但似乎从未使用过 onkey 代码。每当按下后退键时,程序似乎永远不会进入 onKey 方法,无论是在模拟器中还是在设备中。知道我做错了什么吗?

我已经放了更新的代码。

好的,我已经创建了一个干净的新项目。这是代码

package you.packag.namespac;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class BlActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_BACK){
Toast.makeText(this, "Back key pressed", Toast.LENGTH_SHORT);
return true; //that is important
}else
return super.onKeyDown(keyCode, event);
}
}

仍然是同样的问题。没有显示 toast 。每次按下返回时,程序流/控制都会转到 return true 和 return super 语句。真的不明白那应该如何发生。真的让我发疯。必须做点小事我做错了。请帮忙。

最佳答案

尝试

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_BACK){
Toast.makeText(this, "Back key pressed", Toast.LENGTH_SHORT).show();
return true; //that is important
}else
return super.onKeyDown(keyCode, event);
}

或者在你的代码中:

1) 在你的 onKey 方法之前放置 @Override

2) 将return false更改为return super.onKey(keyCode, event)

另外 - 不需要实现 OnKeyListener

关于android - 不进入 Onkeylistener?无法弄清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914363/

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