代码之家  ›  专栏  ›  技术社区  ›  Stelios Papamichail

从具有不同布局的活动访问布局的文本视图

  •  -1
  • Stelios Papamichail  · 技术社区  · 7 年前

    我有一个 mainActivity.java 使用名为 activity_main.xml 和一个名为 drawer_menu.xml 哪个有 textView 显示在应用程序的 drawer menu 我想设置抽屉菜单的 文本视图 文本到 String 我的价值 mainactivity.java(主活动.java) 班级。 如何访问MainActivity类中的该textView?

    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类:

    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(包含要访问的文本视图):

    <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>
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   Ahmed Emad    7 年前

    onCreate() MainActivity.java

    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);
    
        2
  •  0
  •   user8427383    7 年前

        static TextView tv; 
    

        3
  •  0
  •   Sahdeep Singh    7 年前

    here

    <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));
    

    String temp = getResources().getString(R.string.my_string);
    
        4
  •  0
  •   user3064141    7 年前

    推荐文章