这个是android的基础

TextView tv = (TextView) findViewById(R.id.xx);

最近有看到,在api27的AppCompatActivity中,有这样一小段:

@SuppressWarnings("TypeParameterUnusedInFormals")
@Override
public <T extends View> T findViewById(@IdRes int id) {
return

这意味着如果你的编译版本>=27且控件为AppCompatActivity子类,则可不用强制转换,上面代码变成:

TextView tv = findViewById(R.id.xx);

或者可以在父类中自定义:

@Override
public <T extends View> T findViewById(@IdRes int id) {
return

这样可以简化findViewById。