为什么view中的onKeyDown不被执行?view已经获得焦点了……
做一个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>
这个是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>
...全文
相关推荐
C#控件中失去焦点Focus事件与获得焦点Focus事件
C#控件 失去Focus焦点事件与获得焦点事件 文本框失去焦点事件 LostFocus GotFocus事件 供大家学习,减为1分了。。。。
文本框获得焦点和失去焦点的判断代码
文本框失去焦点事件、获得焦点事件 onBlur:当失去输入焦点后产生该事件 onFocus:当输入获得焦点后,产生该文件 Onchange:当文字值改变时,产生该事件 Onselect:当文字加亮后,产生该文件 onpropertychange 当属性改变发生该事件 无论粘贴 keyup onchange等,最为敏感 先来看javascript的直接写在了input上 代码如下:<input name=”pwuser” type=”text” id=”pwuser” class=”input” value=”楼盘账号” onBlur=”if(this.value==”) this.
asp.net中TextBox获得焦点和失去焦点——客户端JavaScript事件
asp.net中TextBox获得焦点和失去焦点——客户端JavaScript事件 自己亲手做的 ,虽然很简单,但是很实用
C# WPF MVVM 实战 – 5- 用绑定,通过 VM 设置 View 的控件焦点
C# WPF MVVM 实战 – 5- 用绑定,通过 VM 设置 View 的控件焦点
详解HTML onfocus获得焦点和onblur失去焦点事件
主要介绍了详解HTML onfocus获得焦点和onblur失去焦点事件的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧


