安卓;对观点感到困惑?

发布于 2024-10-10 07:10:01 字数 839 浏览 5 评论 0原文

我创建了一个类(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2024-10-17 07:10:01

除非您有特定的原因这样做,否则更好的方法可能是让第二个活动(例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文