首页 文章

Android将占位符文本添加到EditText

提问于
浏览
304

如何在不在XML中的类中将placeholder文本添加到 EditText

我的代码中有以下 EditText ,它将显示在alertdialog中:

final EditText name = new EditText(this);

7 回答

  • 708

    这个如何使输入密码具有未转换为* !!的提示 .

    在XML上:

    android:inputType="textPassword"
    android:gravity="center"
    android:ellipsize="start"
    android:hint="Input Password !."
    

    感谢:芒果和rjrjr的见解:D .

  • 6

    android:hint="text" 为用户提供了他需要填写的信息 editText

    例如: - 我有两个edittext,一个用于数值,另一个用于字符串值 . 我们可以为用户设置一个提示,这样他就可以理解他需要给出的 Value

    android:hint="Please enter phone number"
    android:hint="Enter name"
    

    运行应用程序后,这两个edittext将显示输入的提示,点击编辑文本后,用户可以输入他想要的内容(请参阅luxurymode图像)

  • 16

    在你的活动中

    <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:background="@null"
                    android:hint="Text Example"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:id="@+id/name"
                    android:textColor="@color/magenta"/>
    

    enter image description here

  • 0

    喔好吧 . 您正在寻找的是 setHint(int) . 只需从xml中传入字符串的资源ID,就可以了 .

    enter image description here

    编辑

    在XML中,它只是 android:hint="someText"

  • 5

    在Android Studio中,您可以通过GUI添加提示(占位符) . 首先在设计器视图上选择EditText字段 . 然后单击 Component Tree IDE的左侧(通常它在那里,但它可能在那里最小化)在那里你可以看到所选的EditText的 Properties . 找到 Hint 字段如下图所示

    enter image description here

    在那里你可以添加提示(占位符)到EditText

  • 9

    如果您指的是将其添加到布局中的位置 . 您可以定义像FrameLayout这样的容器,并在创建时将此EditText添加到它 .

    <LinearLayout xmlns=".."/>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/container" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    FrameLayout layout = (FrameLayout) findViewById(R.id.container);
    layout.addView(name);
    
  • 2

    If you want to insert text inside your EditText view that stays there after the field is selected (unlike how hint behaves), do This:

    In Java

    // Cast Your EditText as a TextView
    ((TextView) findViewById(R.id.email)).setText("your Text")
    

    In kotlin

    // Cast your EditText into a TextView
    // Like this
    (findViewById(R.id.email) as TextView).text = "Your Text"
    // Or simply like this
    findViewById<TextView>(R.id.email).text = "Your Text"
    

相关问题