如何解决如何避免这些乏味的findViewById调用?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_tile_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample text"
android:textSize="18dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/tv_tile_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="20.0"
android:textSize="26dp"
android:textStyle="bold" />
</LinearLayout>
我在我的应用程序的某些位置包含了此布局。在我的最后一个案例中,我不得不将其包含4次。为了在单独包含的布局中查找文本并将文本设置为这两个文本视图,我必须找到一个ID到一个布局,然后从那里找到一个ID到两个文本视图。并对所有包含的布局重复3次。这导致维护代码有些丑陋和可怕:
@Override
public void setStatsValues(String today,String week,String month,String total) {
// this is so tedious.
View layoutDay = findViewById(R.id.layout_stats_day);
View layoutWeek = findViewById(R.id.layout_stats_week);
View layoutMonth = findViewById(R.id.layout_stats_month);
View layoutTotal = findViewById(R.id.layout_stats_total);
TextView tvDayTitle = layoutDay.findViewById(R.id.tv_tile_title);
TextView tvWeekTitle = layoutWeek.findViewById(R.id.tv_tile_title);
TextView tvMonthTitle = layoutMonth.findViewById(R.id.tv_tile_title);
TextView tvTotalTitle = layoutTotal.findViewById(R.id.tv_tile_title);
TextView tvDayValue = layoutDay.findViewById(R.id.tv_tile_value);
TextView tvWeekValue = layoutWeek.findViewById(R.id.tv_tile_value);
TextView tvMonthValue = layoutMonth.findViewById(R.id.tv_tile_value);
TextView tvTotalValue = layoutTotal.findViewById(R.id.tv_tile_value);
tvDayTitle.setText("Today");
tvWeekTitle.setText("Week");
tvMonthTitle.setText("Month");
tvTotalTitle.setText("Total");
tvDayValue.setText(today);
tvWeekValue.setText(week);
tvMonthValue.setText(month);
tvTotalValue.setText(total);
}
如何避免这种怪诞?
解决方法
您可以使用View Binding:
添加您的build.gradle:
android {
...
buildFeatures {
viewBinding true
}
}
创建布局activity_test.xml
:
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/text1"/>
<TextView
android:id="@+id/text2"/>
</androidx.constraintlayout.widget.ConstraintLayout>
然后在您的Activity
中
科特琳
private lateinit var binding: ActivityTestBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityTestBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.text1.text = "Hello"
binding.text2.text = "....."
}
Java
private ActivityTestBinding binding;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
binding = ActivityTestBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
binding.text1.setText("hello");
}
,
您可以使用View Binding自动完成所有findViewById
的工作-它会生成一个以您的布局命名的“ Binding”类,并且布局中的每个View
都带有一个{{1 }}获得参考。
因此,根据您的情况,您将只能执行id
如果您不想这样做,可以将所有重复的代码重构为一个函数,例如
dayBinding.tv_tile_value.setText("today")
(或任何强制转换为TextView的内容),然后为每个布局ID调用
,实际上,您正在使代码变得乏味,这是您自己的事情。无需先去找父母,然后去找孩子,等等。
代替:
View layoutDay = findViewById(R.id.layout_stats_day);
View layoutWeek = findViewById(R.id.layout_stats_week);
View layoutMonth = findViewById(R.id.layout_stats_month);
View layoutTotal = findViewById(R.id.layout_stats_total);
TextView tvDayTitle = layoutDay.findViewById(R.id.tv_tile_title);
TextView tvWeekTitle = layoutWeek.findViewById(R.id.tv_tile_title);
TextView tvMonthTitle = layoutMonth.findViewById(R.id.tv_tile_title);
TextView tvTotalTitle = layoutTotal.findViewById(R.id.tv_tile_title);
TextView tvDayValue = layoutDay.findViewById(R.id.tv_tile_value);
TextView tvWeekValue = layoutWeek.findViewById(R.id.tv_tile_value);
TextView tvMonthValue = layoutMonth.findViewById(R.id.tv_tile_value);
TextView tvTotalValue = layoutTotal.findViewById(R.id.tv_tile_value);
您可以这样做(只是不同的ID,我的意思是每个视图使用不同的ID):
TextView tvDayTitle = findViewById(R.id.tv_day_title);
TextView tvWeekTitle = findViewById(R.id.tv_week_title);
TextView tvMonthTitle = findViewById(R.id.tv_month_title);
TextView tvTotalTitle = findViewById(R.id.tv_total_title);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。