Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一行中多次查找视图_Java_Android_Textview_Findviewbyid - Fatal编程技术网

Java 在一行中多次查找视图

Java 在一行中多次查找视图,java,android,textview,findviewbyid,Java,Android,Textview,Findviewbyid,这不是一个问题,而是一个效率问题。我在Android应用程序的XML布局中有多个文本视图(其中两个)。我的问题是,我可以在一行中选择多个文本视图,findViewById多个文本视图吗 这对我的问题有效吗 TextView title, darkThemeTitle = findViewById(R.id.title); findViewById(R.id.darkThemeTitle); 你试过用黄油刀吗?此库帮助您进行依赖项注入,因此您无需担心findviewbyd。只需调用@BindVi

这不是一个问题,而是一个效率问题。我在Android应用程序的XML布局中有多个文本视图(其中两个)。我的问题是,我可以在一行中选择多个文本视图,
findViewById
多个文本视图吗

这对我的问题有效吗

TextView title, darkThemeTitle = findViewById(R.id.title); findViewById(R.id.darkThemeTitle);

你试过用黄油刀吗?此库帮助您进行依赖项注入,因此您无需担心
findviewbyd
。只需调用
@BindView(view\u id)
以及要绑定的变量的类型和名称

@BindView(R.id.title)
TextView title;
@BindView(R.id.darkThemeTitle)
TextView darkThemeTitle;
请记住,您需要在
build.gradle
文件中添加依赖项

compile 'com.jakewharton:butterknife:8.8.1'
并在活动的
onCreate
中调用bind方法

ButterKnife.bind(this);

唯一的建议是使用模板ID查找视图:

TextView[] themedViews = new int[NUMBER_OF_THEMES];
for (int k = 0; k < NUMBER_OF_THEMES; k++)
    themedViews[k] = findViewById(context.getResources().getIdentifier("some_prefix" + String.valueOf(k), "id", packageName));
TextView[]主题视图=新整数[主题数量];
for(int k=0;k<主题数;k++)
主题视图[k]=findviewbyd(context.getResources().getIdentifier(“some_前缀”+String.valueOf(k),“id”,packageName));
这将查找当前活动的所有视图


或者您可以使用
parent.findViewById
查找指定视图的子视图。

当您使用
TextView标题时,darkThemeTitle=findViewById(R.id.title);findViewById(R.id.darkthemetile)在您的代码中

  • 这一行
    TextView标题,暗色调=(TextView)findViewById(R.id.title)将显示变量“title”可能尚未初始化。因此
    title
    从未在代码中初始化过

  • findviewbyd(R.id.tab\u布局)将在代码中返回视图。并且它永远不会在代码中返回
    darkThemeTitle

你可以这样做

 TextView title = (TextView) findViewById(R.id.title); TextView darkThemeTitle = (TextView) findViewById(R.id.darkThemeTitle);
另一种方式

TextView title = null, darkThemeTitle = null;

TextView[] textViews = {title, darkThemeTitle};
Integer[] ids = {R.id.title, R.id.darkThemeTitle};

for (int i = 0; i < textViews.length; i++) {
    textViews[i] = (TextView) findViewById(ids[i]);
}
TextView title=null,darkThemeTitle=null;
TextView[]TextView={title,darkThemeTitle};
整数[]id={R.id.title,R.id.darkThemeTitle};
对于(int i=0;i
我不鼓励您这样做,因为其他程序员更难阅读,也不会为您节省很多打字时间。使用:

TextView title = findViewById(R.id.title), darkThemeTitle = findViewById(R.id.darkThemeTitle);

只是好奇,为什么你需要它在一行?单行代码与多行代码的主要区别是什么?为了尽量减少将代码拆分成方法的数量,请将findview代码移到主代码之外,使用折叠。我无法理解水平编码与垂直编码相比的优势。您的屏幕和IDE设计为垂直。我可以实际推荐的是-使用动态绑定,请查看我的答案了解详细信息。谢谢,但是有可能像这样发布视图吗<代码>文本视图标题,暗色调=findViewById(R.id.title);findViewById(R.id.darkthemetile)在所有控件实例化后,
ButterKnife.bind(this)
不是会去吗?如果你在
活动中使用
ButterKife.bind()
,你可以在
onCreate
中使用,如果这更复杂,比如
ViewHolder
Fragment
,你需要使用
ButterKnife>(此,parentView/activityInstance)
。通过
parentView
可以膨胀新布局或传递
activityInstance
。因此这将实例化XML布局中的所有视图?我更新了答案,我以前的代码只是查找视图ID,我将其更改为查找视图,顺便说一句,我不建议将其用于查找主题控件。最佳解决方案:移动您的findViewById命令用于帮助器类中的分离方法。