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

 时间:2024-10-14 22:17:16

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布局文件的控件
  • PHP中使用FOR/FOREACH遍历二维数组
  • Python编程:怎么设置tkinter窗口最大尺寸
  • C语言实现随机序列
  • python中如何对字典进行排序
  • java怎么输入数字
  • 热门搜索
    寻访红色足迹手抄报 向国旗敬礼手抄报内容 低碳生活手抄报图片 教师节英语手抄报内容 感恩父母手抄报资料 迎元旦手抄报 古诗配画手抄报 健康手抄报资料 愚人节手抄报 我心中的红十字手抄报