当前位置: 首页 > 知识库问答 >
问题:

在Android中设置内布局中的文本字段的值[重复]

山高峰
2023-03-14

我想设置内部布局中文本字段的值。这是我的父xml(外部布局)。

活动导航器。xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" >

     </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

导航总管总管。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/ic_background"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <TextView
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:id="@+id/userTextView"
        android:text="text"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>

我想从navigator设置文本字段userTextView的值。kt级

navigator.kt

       override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_navigator)
//getting exception here
                  val userTextView: TextView= findViewById<TextView>(R.id.userTextView)
           Log.d("usertextview",userTextView.toString())

                }

请帮我解决这个问题。

共有1个答案

夏星阑
2023-03-14

在你的navigator.kt试试这个

获取导航Headerview,并将文本设置为TextView。像这样

    lateinit var  navView:View
    lateinit var  txt_name : TextView

    navView = nav_view.getHeaderView(0)

    txt_name = navView.findViewById(R.id.userName) as TextView
    txt_name.text = "user"
 类似资料:
  • 问题内容: 我正在用nodejs和html进行项目。任何人都可以帮助您从server.js中设置html文本字段的值。例如,我在index.html上有ID为’name’的文本字段。我使用res.body.name =’nametest’。但是它不起作用。请给我一个简单的例子。谢谢朋友 问题答案: 为了设置服务器中的字段,您需要将其设置为在发送HTML时定义的预设值,或者稍后再动态设置。第一个选项

  • 我想将两个项目对齐到另一个元素,但我有问题。 现在,如果我将顶部文本约束到left元素的顶部,将底部文本约束到left元素的底部,并将它们链接起来。整个文本就会移动。我想修复顶部的文本元素,但如果文本较少,仍将其居中

  • 我在动态应用程序中使用片段,其中User.java文件包含值,而< code>TabbedActivity.java文件包含三个片段。我想将文本设置为< code > profile rgament . Java 中的< code>TextView。因此,我在< code > fragment _ profile . XML 中创建了一个< code>TextView,并使用以下代码从< code

  • 如何在JavaFX中设置的宽度? 我试过这个: 但我看不到任何变化。

  • 问题内容: 我正在尝试在Java FX中填充文本字段的值。 我有Main Class,controller和fxml。我已经将fxml文件与controller和其中的适当字段绑定在一起。当我尝试设置其值时,它会失败。 Main.java testController.java FXML文件: 问题答案: 您在错误的位置进行操作!如果您需要在加载fxml之前使用控件,则需要在中进行操作。为此,您的

  • eclipse布局文件中的Android屏幕设计与设备或模拟器上的显示方式不同。当我第一次设计布局时,它会工作,但突然所有的布局都在eclipse上损坏了,但在真正的手机和模拟器上没有 损坏的设计: 它在手机上的表现: 有人经历过这种情况吗? 谢谢你的帮助。