安卓;对观点感到困惑?
我创建了一个类(InputControl),它扩展了我的主类(Main)的视图,并占据了屏幕的焦点。我在主 xml 布局上有一个按钮,它调用 control() 并设置我的 InputControl 视图,从那里我捕获用户输入。
如何从 InputControl 视图类返回到 xml 布局?
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputControl = new InputControl(this);
}
//......SNIP!
public void control(){
setContentView(InputControl);
InputControl.requestFocus();
}
}
public class InputControl extends View implements OnTouchListener {
public InputControl(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
//...I AM CAPTURING USER TOUCH EVENTS HERE
}
}
I have created a class (InputControl) which extends the view of my main class (Main), and takes focus of the screen. I have a button on the main xml layout which calls control() and sets up my InputControl view, from there I capture user input.
How can I return back to the xml layout from the InputControl view class?
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputControl = new InputControl(this);
}
//......SNIP!
public void control(){
setContentView(InputControl);
InputControl.requestFocus();
}
}
public class InputControl extends View implements OnTouchListener {
public InputControl(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
//...I AM CAPTURING USER TOUCH EVENTS HERE
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您有特定的原因这样做,否则更好的方法可能是让第二个活动(例如 InputControlActivity)拥有自己的布局文件,并将您的 InputControl 类嵌入其中。
然后,您将启动第二个活动的实例(使用 startActivity()) - 一旦您完成了 InputControlActivity,只需按手机的“后退”按钮即可关闭该活动并返回到主活动。
Unless you have have a specific reason for doing things this way, a better way might be to have a second activity (InputControlActivity for example) with its own layout file and embed your InputControl class into that.
You would then start an instance of the second activity (with startActivity()) - once you're finished in the InputControlActivity, simply pressing the phone's BACK button will close that activity and return to the main one.