android - 在 Android::when setContentView() 被替换为新布局时,在运行时调用哪个方法

标签 android android-layout android-widget

大家好,我是 Android 编程的新手。 我的问题是... 我设计了 2 个布局。

1) R.layout.main:由一个TextField、一个EditText和一个Button组成

2) R.layout.window:它有 5 个按钮

这里首先显示的是 R.layout.main。默认情况下,R.layout.main 正在调用,因此当我按下该主布局的按钮时,它需要转发到 r.layout.window。 转发过程ok..

但是当我打电话时 BUTTON button=(Button)findViewById(R.id.b1);//b1 在 R.layout.window 上

我遇到了一个异常..所以我可以知道当调用 setContentView() 和从 ONCREATE() 调用哪个方法时,是否可以调用多个布局 R.id.namess..

等待回复

最佳答案

public class UI extends Activity {
    LayoutInflater factory 
    View layout1;
    View layout2;    
public void onCreate(Bundle bundle){
 .....
 .....

 factory =getLayoutInflater();
 layout1 =  factory.inflate(R.layout.main, null);
 layout2 =  factory.inflate(R.layout.window, null);
.....
..... 
   }
}



//access R.id.b1 in R.layout.window;

试试这个

Button button=(Button)layout2.findViewById(R.id.b1);

如果像这样将内容 View 设置为 R.layout.main setContentView(R.layout.main); 然后 你只能像这样通过 findViewById() 访问 R.layout.main 中的小部件

Button b = (Button)findViewById(R.id.button1);

R.id.button1R.layout.main 中然后当你完成 R.layout.main 设置内容 View 窗口布局,然后访问其中的小部件

setContentView(R.layout.window);

//now you can access elements in window layout using findViewById
Button b2  =(Button)findViewById(R.id.button2); 

希望你明白

关于android - 在 Android::when setContentView() 被替换为新布局时,在运行时调用哪个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382442/

相关文章:

android - 在 fragment 中使用资源

android - 什么是通知的 "small icon"mdpi 大小

android - 如何改变RayMenu的方向

android - 我们可以在 Android MotionLayout 的同一个 <Transition> 中使用 OnSwipe 和 OnClick 吗?

android - 如何使动画 GIF 从 Android WebView 工作

android - 为移动设备创建链接的正确方法是什么?

Android facebook Intent 显示类 com.facebook.katana.ProfileTabHostActivity 的个人资料不再起作用

android - Lollipop 的 backgroundTint 对 Button 没有影响

android - 带有联系人照片的联系人列表会造成性能问题

android - 为什么 Marquee 在 Widget 中不起作用?