简体   繁体   English

来自其他类的findViewById

[英]findViewById from a different class

This should be simple, but even reading other solutions I can't get it... I simply want to access a TextView with findViewById from a different class (it's in a different file), how to do that? 这应该很简单,但是即使阅读其他解决方案我也无法理解...我只是想从不同的类(在不同的文件中)访问带有findViewById的TextView,该怎么做?

MainActivity : MainActivity

public class MainActivity extends ActionBarActivity {

    private TextView label0 = (TextView) findViewById(R.id.label0);
    MyDialog d = new MyDialog(this);

    public MainActivity(){

    }

    //other methods and stuff...
}

Here is the DialogFragment that must access the TextView: 这是必须访问TextView的DialogFragment

public class MyDialog extends DialogFragment {

    private Activity MainActivity;
    private TextView label0;

    public MyDialog(Activity parent) {
    this.MainActivity = parent;
    }

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.alert_content)
           .setPositiveButton(R.string.alert_button, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

               //here it is, trying to access label0. This is one of the many tries
               label0 = MainActivity.findViewById(R.id.label0);

               //do other stuff...
}

I also read many times to never make the views "public static", Is that right? 我也读了很多遍,从来没有使观点成为“公共静态”,是吗?

  1. There is no need for constructor for Activity class. Activity类不需要构造函数。

  2. Fragments must have no arg constructor 片段必须没有arg构造函数

  3. The below in Activity is wrong 活动中的以下内容有误

    private TextView label0 = (TextView) findViewById(R.id.label0); MyDialog d = new MyDialog(this);

  4. The fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout. 该片段可以使用getActivity()访问Activity实例,并轻松执行诸如在活动布局中查找视图之类的任务。 Read Communicating with activity @ developer.android.com/guide/components/fragments.html 阅读@ 与活动进行通信 @ developer.android.com/guide/components/fragments.html

save the textview as a private property of activity and expose it as getters for fragment. 将textview保存为活动的私有属性,并将其公开为片段的获取器。 Thats the standard way to do it. 多数民众赞成在这样做的标准方法。 You will need to cast fragment.getActivity() to your activity type and then access the textviewas a getter. 您将需要将fragment.getActivity()强制转换为您的活动类型,然后以获取方式访问textview。

Edit---------------- in the main activity you already have private TextView label0 just fill its value in onCreateView . 在主要活动中,您已经拥有private TextView label0 Edit ----------------只需将其值填充在onCreateView Then provide a getter method in your activity to access this textView from outside the activity. 然后在您的活动中提供一个getter方法,以从活动外部访问此textView。

public TextView getLabelTextView(){
    return this.label0;
}

Now in your fragment do something like this: 现在在您的片段中执行以下操作:

Activity activity = this.getActivity();
MainActivity myActivity = (MainActivity)activity;
TextView tv = myMactivity.getLabelTextView();

And you should be good to go. 而且您应该很好。

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

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