English 中文(简体)
Anders Add placeholder text to EditText
原标题:Android add placeholder text to EditText

How can I add a placeholder text to EditText in the class that isn t in the XML?

我的守则有以下<代码>EditText,将在警示中显示:

    final EditText name = new EditText(this);
最佳回答

Ah, ok。 您回顾的是:setHint(int)。 简单地从你的xml中抽取资源,然后回去。

enter image description here

EDIT

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

问题回答

android:hint=“text”>为用户提供了一个信息,说明他需要特别填写editText

例如: i 具有两个数字数值,另一个是显示价值。 我们可以为用户打上灯,以便他能够理解他需要给予什么价值。

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

这两条ed字之后,在点击 text后,将显示所进入的 h,用户可以进入他想要的东西(见奢侈品图像)。

In your Activity

<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

You have to use the android:hint attribute

<EditText
android:id="@+id/message"
android:hint="<<Your placeholder>>"
/>

In Android Studio, you can switch from XML -> Design View and click on the Component in the layout, the EditText field in this case. This will show all the applicable attributes for that GUI component. This will be handy when you don t know about all the attributes that are there.

你们会感到惊讶的是,EditText有140多个定制特性。

如何提出附带内容的字句,不要改写!

关于XML:

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

亲眼见:D.

In Android Studio you can add Hint (Place holder) through GUI. First select EditText field on designer view. Then Click on Component Tree Left side of IDE (Normally it s there, but it may be there minimized) There you can see Properties of selected EditText. Find Hint field as below Image

enter image description here

您可向EditText增加Hint(Place持有人)

If you mean the location where you will add it in the layout. You can define a container like a FrameLayout and add this EditText to it when it is created.

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

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

Java:

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

在科特林:

// 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"




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签