android 如何获得其他xml布局文件的控件

2025-05-11 19:55:22

1、想要再MainAcitivty中获取activity_main以外的布局文件:dialog.xml获取TextView1的内容“长度”二字。光光TextView tv = (TextView) findViewById(R.id.textView1);这句话根本无法获取。

android 如何获得其他xml布局文件的控件

2、在oncreate()方法里这时用到LayoutInflater来加载布局共4句话议陬涸渎即可获取//用LayoutInflater加载布局 LayoutInflater factory = LayoutInflater.from(MainActivity.this); //获取dialog布局文件获取View final View textEntryView = factory.inflate(R.layout.dialog, null); // 通过textEntryView来获取控件 final TextView tv1 = (TextView) textEntryView .findViewById(R.id.textView1); Toast.makeText(MainActivity.this, tv1.getText(), Toast.LENGTH_SHORT).show();

android 如何获得其他xml布局文件的控件

3、此时,界面就能获取dialog布局文件中 TextView的值了

android 如何获得其他xml布局文件的控件
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢