简体   繁体   English

是否有任何解决方案可以在不同的方法中仅使用 1 个 findViewById

[英]Is there any solution to use only 1 findViewById in different methods

I have 2 different methods.我有两种不同的方法。

Method a will display textview1.方法a 将显示textview1。 Method b will also display textview1.方法 b 也会显示 textview1。

In both the methods, I need to include findViewById(R.id.textview1).在这两种方法中,我都需要包含 findViewById(R.id.textview1)。 Thus need to put twice findViewById in the code.因此需要在代码中放置两次 findViewById。

// if you are using Kotlin, apply this plugin with the kotlin library in your app Module Gradle // 如果您使用的是 Kotlin,请在您的应用模块 Gradle 中将此插件与 kotlin 库一起应用

apply plugin: 'kotlin-kapt'

kapt {
    generateStubs = true
}

// then you can just give the name of the view in the XML layout and you dont have to initialize the view eg .. // 然后你可以在 XML 布局中给出视图的名称,而不必初始化视图,例如 ..

// xml view // xml 视图

<TextView
                        android:id="@+id/tv_location"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:gravity="center_vertical"
                        android:paddingStart="8dp"
                        android:singleLine="true"

                        android:textSize="14sp" />

// now in the kotlin class // 现在在 kotlin 类中

tv_location.text = "Kolkata,India" 

//try this 

Use butterknife library:使用黄油刀库:

class Activity {
    @BindView(R.id.textview1)
    TextView view1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    }

You can now access view1 in any methods in this activity.您现在可以在此活动中以任何方法访问view1

You can declare your textview as global variable.您可以将 textview 声明为全局变量。 Then you can use that variable anywhere in that activity or class or fragment.然后您可以在该活动或类或片段中的任何位置使用该变量。

public class Main2Activity extends AppCompatActivity {

   private TextView textView1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main2);
      textView1 = findViewById(R.id.textview1);
   }
   private void method1(){
      textView1.setText("method 1");
   }
   private void method2(){
      textView1.setText("method 2");
   }
}

ButterKnife is developed by Jake Wharton at Square and is essentially used to save typing repetitive lines of code like findViewById(R.id.view) when dealing with views, thus making our code look a lot cleaner. ButterKnife 由 Square 的 Jake Wharton 开发,主要用于在处理视图时避免输入重复的代码行,例如 findViewById(R.id.view),从而使我们的代码看起来更简洁。

add this dependency into your build.gradle将此依赖项添加到您的 build.gradle 中

compile 'com.jakewharton:butterknife:6.1.0'

write this on onCreate()在 onCreate() 上写这个

ButterKnife.bind(this);

and than you can find you elements like this :然后你可以找到这样的元素:

@Bind(R.id.txtview1)
Textview textview;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM