为什么view中的onKeyDown不被执行?view已经获得焦点了……

phoenix_am 2011-06-21 11:13:18
做一个activity,上面显示一个按钮1,做一个view,里面是linearLayout,也包含一个按钮2,点击按钮1,显示view,但是view的onKeyDown方法为什么不被调用?我确定view已经requestfocus了,因为打印view.isfocusable结果是true。有没有高手遇到过这种情况?
这个是activity的代码
public class SimpleTestView extends Activity {
/** Called when the activity is first created. */

private LayoutInflater inflater;
private LinearLayout layout;
private View view_main;
private TextView mtext;
private Button mBtn,mBton2;
View view_test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

inflater = (LayoutInflater) this
.getSystemService(SimpleTestView.LAYOUT_INFLATER_SERVICE);
view_main = inflater.inflate(R.layout.main, null);
layout = (LinearLayout)view_main.findViewById(R.id.main_layout);
setContentView(view_main);
mtext = (TextView)view_main.findViewById(R.id.main_txt);
mBtn =(Button) view_main.findViewById(R.id.main_btn);
SimpleTest testview = new SimpleTest(SimpleTestView.this);
view_test=testview.getView();
mBton2=testview.getButtonView();
mBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
layout.addView(view_test);
mBton2.setFocusable(true);
mBton2.requestFocus();
System.out.println("sunny"+view_test.isFocusable());
}
});

}
}
这个是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/main_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/main_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

下面是view的代码
public class SimpleTest extends View {
private View view_test;
private Button myBtn;
public SimpleTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
view_test = new View(this.getContext(), null);
LayoutInflater dialoginflater = LayoutInflater.from(this.getContext());
view_test = dialoginflater.inflate(R.layout.view, null);
myBtn = (Button)view_test.findViewById(R.id.my_button);
myBtn.setText("OK!!!!!!");
this.setFocusable(true);
myBtn.setFocusable(true);
myBtn.requestFocus();
}
/** Called when the activity is first created. */

public View getView()
{
return view_test;
}
public Button getButtonView()
{
return myBtn;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("sunny, i'm here");
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_1:
case KeyEvent.KEYCODE_MENU:
myBtn.setText("Middle");

}
return true;
}
}
view的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="600px"
android:layout_height="60px"
android:focusable="true"
android:background="#aa00aa00"
>

<Button
android:id="@+id/my_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:focusable="true"
/>

</LinearLayout>
...全文
688 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
念茜 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 phoenix_am 的回复:]
这个问题已经解决了。我本来的意图是想要在自定义的SimpleTest 这个view里对按键进行控制,但是我对view的概念理解的有误,实际上在程序里我并没有用SimpleTest 创建的testview 对象进行addview,而是将view.xml文件中的view inflate出来,定义为view_test,实际addview的时候add是这个view,而不是SimpleTest 创建的对象。……
[/Quote]

原来是这样
并没有加载SimpleTest对象
kangnixi 2011-06-21
  • 打赏
  • 举报
回复
额。。。看了楼主的代码,逻辑很混乱。。。。

而且你是在 layout里面 addView的,也就是说你要捕捉按键的事件

猜测捕捉按键的事件,应该写在activity才对吧?

===
个人博客:www.girlcoding.com
phoenix_am 2011-06-21
  • 打赏
  • 举报
回复
重新编辑一下代码,已经加入了setFocusableInTouchMode(true);仍然无效
这个是activity的代码

public class SimpleTestView extends Activity {
/** Called when the activity is first created. */

private LayoutInflater inflater;
private LinearLayout layout;
private View view_main;
private TextView mtext;
private Button mBtn,mBton2;
View view_test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

inflater = (LayoutInflater) this
.getSystemService(SimpleTestView.LAYOUT_INFLATER_SERVICE);
view_main = inflater.inflate(R.layout.main, null);
layout = (LinearLayout)view_main.findViewById(R.id.main_layout);
setContentView(view_main);
mtext = (TextView)view_main.findViewById(R.id.main_txt);
mBtn =(Button) view_main.findViewById(R.id.main_btn);
SimpleTest testview = new SimpleTest(SimpleTestView.this);
view_test=testview.getView();
mBton2=testview.getButtonView();
mBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
layout.addView(view_test);
view_test.requestFocus();
view_test.requestFocusFromTouch();
mBton2.setFocusable(true);
mBton2.requestFocus();
mBton2.setText("WHy");
System.out.println("sunny"+view_test.isFocusable());

}
});



}
}

这个是mail.xml的代码

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

这个是view的代码

public class SimpleTest extends View {
private View view_test;
private Button myBtn;
public SimpleTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
view_test = new View(this.getContext(), null);
LayoutInflater dialoginflater = LayoutInflater.from(this.getContext());
view_test = dialoginflater.inflate(R.layout.view, null);
myBtn = (Button)view_test.findViewById(R.id.my_button);
myBtn.setText("OK!!!!!!");
this.setFocusable(true);
this.setFocusableInTouchMode(true);
myBtn.setFocusable(true);
myBtn.requestFocus();
}
/** Called when the activity is first created. */

public View getView()
{
return view_test;
}
public Button getButtonView()
{
return myBtn;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("sunny, i'm here");
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_1:
case KeyEvent.KEYCODE_MENU:
myBtn.setText("Middle");
this.invalidate();

}
return true;
}
}

这个是view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="600px"
android:layout_height="60px"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#aa00aa00"
>

<Button
android:id="@+id/my_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:focusable="true"
/>

</LinearLayout>
phoenix_am 2011-06-21
  • 打赏
  • 举报
回复
不好意思第一次发帖,不会编辑格式。但是我加入setFocusableInTouchMode(true);也无效。
scttsc_qxl 2011-06-21
  • 打赏
  • 举报
回复
格式太乱了,如果我没看错的话,我觉得你还应该
在子view中设置:
setFocusableInTouchMode(true);
儿大不由爷 2011-06-21
  • 打赏
  • 举报
回复
是被别的地方收了把
phoenix_am 2011-06-21
  • 打赏
  • 举报
回复
这个问题已经解决了。我本来的意图是想要在自定义的SimpleTest 这个view里对按键进行控制,但是我对view的概念理解的有误,实际上在程序里我并没有用SimpleTest 创建的testview 对象进行addview,而是将view.xml文件中的view inflate出来,定义为view_test,实际addview的时候add是这个view,而不是SimpleTest 创建的对象。也就是说,SimpleTest 这个class创建的对象根本没有用到,所以当然也不会进入onkeydown和ondraw。
解决的方法是1.在SimpleTest 类的ondraw里实现想要画的内容,在activity里addview SimpleTest 类的对象,这样就可以进入view的onkeydown了。
2.直接在activity里inflate view.xml,取消SimpleTest 这个类,在activity里的onkeydown处理按键。

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧