做一个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>
在子view中设置:
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>
个人博客:www.girlcoding.com
解决的方法是1.在SimpleTest 类的ondraw里实现想要画的内容,在activity里addview SimpleTest 类的对象,这样就可以进入view的onkeydown了。
2.直接在activity里inflate view.xml,取消SimpleTest 这个类,在activity里的onkeydown处理按键。
原来是这样
并没有加载SimpleTest对象