Android如何加载新布局
我试图在单击按钮时加载新的 layout
,该布局中只有一个 webView
所以我的目标是,当单击按钮时它会打开webView 并将用户引导到预先确定的页面。现在我
Button codesBtn = (Button)findViewById(R.id.imagebutton1);
codesBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.codes);
}
});
在主要活动的 onCreate()
方法中找到了 This 。我有几个问题:
1)这是放置此代码块的正确位置吗?
2) 我是否需要为 webView 创建一个单独的活动以及我想要按钮执行的操作?
3)如果是,所需活动的基本结构是什么?
提前致谢!
I'm trying to load a new layout
when I click a button, this layout has only a webView
in it so my goal is, when the button is clicked that it opens the webView and directs the user to a pre-determined page. Right now I have
Button codesBtn = (Button)findViewById(R.id.imagebutton1);
codesBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.codes);
}
});
This is inside my onCreate()
method in my main activity. I have a couple concerns:
1) is this the correct place to put this block of code into?
2) Do I need to create a separate activity for the webView and what I want the button to do?
3) If so, what is the basic structure of the activity needed?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,使用新布局启动新活动比更改当前活动中的布局更容易。
如果您想将用户定向到某个网站,您可以使用意图来要求浏览器打开(示例取自 这个问题)
或者,您可以创建一个只有 WebView 的 Activity 并通过以下方式启动它:
In general, rather than changing the layout in the current activity it is easier to launch a new activity with the new layout.
If you want to direct the user to a website, you could use an intent to ask the browser to open (example taken from this question)
Or, you could create an Activity that just has a WebView and launch that by saying;