从具有不同布局的 Activity 访问布局的 TextView

Accessing a layout's TextView from an Activity with a different layout

我有一个 mainActivity.java class 的布局名为 activity_main.xml 和一个名为 drawer_menu.xml 的 .xml 文件,其中有一个 textView显示在应用程序的 drawer menu 中。我想将抽屉菜单的 textView 文本设置为我在 mainActivity.java class 中的 String 值。 如何访问 mainActivity 中的 textView class?

activity_main.java(访问textView的部分):

    /**
 * Prompts the user for his/her username
 * when the tutorial is done
 */
private void promptForUsername() {
    UsernameDialog dialog = new UsernameDialog();
    dialog.setCancelable(false);
    dialog.show(getFragmentManager(),"USERNAME_DIALOG");
    username.setText(getUsername());
}

public void setUsername(String name) {
    tempName = name;
}

public String getUsername() {
    return tempName;
}

UsernameDialog.java class:

public class UsernameDialog extends DialogFragment {

@BindView(R.id.editText) EditText mEditText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // inflate the layout using the dialog themed context
    final Context context = getActivity();
    final LayoutInflater inflater = LayoutInflater.from(context);
    final View view = inflater.inflate(R.layout.username_dialog,null,false);

    final MainActivity activity = new MainActivity();

    ButterKnife.bind(this,view);

    DialogInterface.OnClickListener posListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            activity.setUsername(mEditText.getText().toString());
            Log.d("USERNAME",mEditText.getText().toString());
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
            .setTitle("Choose your username")
            .setView(view)
            .setPositiveButton("OK",posListener);
    return builder.create();
}
}

drawer_menu.xml(包含要访问的TextView):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/NavigationDrawerHeader">

<ImageView
    android:id="@+id/menu_logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="18dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/logo"
    app:srcCompat="@drawable/user_icon" />

<TextView
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="66dp"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/menu_logo"
    android:layout_marginStart="93dp"
    android:layout_marginTop="5dp"
    android:fontFamily="@font/alegreya_sans_extrabold"
    android:paddingTop="25dp"
    android:text="username"
    android:textColor="#ffffff"
    android:textSize="18sp" />

</RelativeLayout>

你可以定义

    static TextView tv; 

并且可以从其他 class

访问

没有人推荐使用静态视图。查看 here 了解更多。

现在,根据您的情况,您可以创建一个字符串资源,以便能够在两个活动中使用相同的文本。怎么做?这里:

  • 在您的 strings.xml 中添加这样的字符串
<string name="my_string">what ever u want to put here </string>
  • 添加文本视图
 TextView textView = (TextView) findViewById(R.id.Id_of_TextView);
 textView.setText(getString(R.id.my_string));

您可以直接在两个文本视图中添加该字符串 xml。

要获取变量中的字符串值,您可以使用

String temp = getResources().getString(R.string.my_string);

如果您在 xml 布局文件中为文本视图指定一个 ID,则可以使用 TextView myTextView = (TextView) findViewById(R.id.myTextView);

MainActivity.java

中的 onCreate() 方法末尾尝试此代码
NavigationView navigationView = findViewById(R.id.nav_view);
//R.id.nav_view the id of the navigation drawer

View drawerHead = navigationView.getHeaderView(0);
//0 index of the header

TextView userName = drawerHead.findViewById(R.id.username);