Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在中心的edittext中将文本和图像设置为提示?_Android_Xml_Android Layout - Fatal编程技术网

Android 如何在中心的edittext中将文本和图像设置为提示?

Android 如何在中心的edittext中将文本和图像设置为提示?,android,xml,android-layout,Android,Xml,Android Layout,我想像这样在中心编辑文本时将图像和文本设置为提示 我的XML代码是 <EditText android:layout_width="300dp" android:layout_height="40dp" android:id="@+id/ed_username" android:layout_marginBottom="152dp" android:hint="@string/username" android:drawableLeft="@drawable/username_ima

我想像这样在中心编辑文本时将图像和文本设置为提示

我的XML代码是

<EditText  android:layout_width="300dp" android:layout_height="40dp" 
android:id="@+id/ed_username" android:layout_marginBottom="152dp" 
android:hint="@string/username"
android:drawableLeft="@drawable/username_image" 
android:textColorHint="@color/skyblue" 
 android:background="@drawable/edittext_shape" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:ellipsize="start" android:gravity="center|center_horizontal"/>


请帮助我提前感谢a

您可以在edittext中将图像添加为drawableleft,并在获取焦点时将其删除

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:drawableLeft="@drawable/yourimage" />

您可以在edittext中将图像添加为drawableleft,并在获取焦点时将其删除

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:drawableLeft="@drawable/yourimage" />
你可以通过两种方式做

1.像这样创建编辑文本

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:hint="Search text"
        android:id="@+id/editText"
        android:background="@drawable/edit_text_bottom_border"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingLeft="15dp" 
        android:drawablePadding="10dp"     
        android:drawableLeft="@android:drawable/ic_search_category_default"/>

在布局中创建此文件

编辑文本底部边框.xml

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="1dp"
            android:left="-3dp"
            android:right="-3dp"
            android:top="-3dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="2dp"
                    android:color="#000" />

                <solid android:color="#00ffffff" />

            </shape>
        </item>
    </layer-list>

2.如下图所示创建相对值:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/editText" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:src="@android:drawable/btn_star_big_on"/>

        <EditText
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="46dp"
            android:hint="hint"
            android:layout_alignParentRight="true" />

        <View
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="-7dp"
            android:layout_below="@+id/text"
            android:background="#000"/>
</RelativeLayout>

生成的输出为:

您可以通过双向

1.像这样创建编辑文本

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:hint="Search text"
        android:id="@+id/editText"
        android:background="@drawable/edit_text_bottom_border"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:paddingLeft="15dp" 
        android:drawablePadding="10dp"     
        android:drawableLeft="@android:drawable/ic_search_category_default"/>

在布局中创建此文件

编辑文本底部边框.xml

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="1dp"
            android:left="-3dp"
            android:right="-3dp"
            android:top="-3dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="2dp"
                    android:color="#000" />

                <solid android:color="#00ffffff" />

            </shape>
        </item>
    </layer-list>

2.如下图所示创建相对值:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/editText" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:src="@android:drawable/btn_star_big_on"/>

        <EditText
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="46dp"
            android:hint="hint"
            android:layout_alignParentRight="true" />

        <View
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="-7dp"
            android:layout_below="@+id/text"
            android:background="#000"/>
</RelativeLayout>

生成的输出为:


您可以在java文件中更改如下所示的提示颜色

setHintTextColor(getResources().getColor(R.color.Red))

可以使用以下属性将drawlable设置为编辑文本

android:drawableLeft="@drawable/ic_launcher"             
 android:drawableTop=""              
 android:drawableBottom=""    
 android:drawableStart=""     
 android:drawableEnd=""    
 android:drawableRight=""
但不幸的是,不能像这样在编辑文本的中心设置drawlable,
或者您可以使用背景图像,其中包括您显示的提示+图像以及编辑文本中的内容。addTextChangedListener您可以删除以前应用的backgrund。

您可以在java文件中更改提示颜色,如下所示

setHintTextColor(getResources().getColor(R.color.Red))

可以使用以下属性将drawlable设置为编辑文本

android:drawableLeft="@drawable/ic_launcher"             
 android:drawableTop=""              
 android:drawableBottom=""    
 android:drawableStart=""     
 android:drawableEnd=""    
 android:drawableRight=""
但不幸的是,不能像这样在编辑文本的中心设置drawlable,
或者您可以使用背景图像,其中包含您显示的任何内容以及编辑文本中的提示+图像。addTextChangedListener您可以删除以前应用的backgrund。

您可以尝试使用
spannable
设置提示,然后您不必更改XML或布局。这是一个文本视图的示例。我从未尝试过使用
setHint()
,但我认为它会以同样的方式工作

TextView tv = (TextView)findViewById(R.id.yourTextView);
SpannableStringBuilder span = new SpannableStringBuilder( "  Username" );
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
span.setSpan(new ImageSpan(icon), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
tv.setText(span, BufferType.SPANNABLE);

您可以尝试使用
spannable
设置提示,这样就不必更改XML或布局。这是一个文本视图的示例。我从未尝试过使用
setHint()
,但我认为它会以同样的方式工作

TextView tv = (TextView)findViewById(R.id.yourTextView);
SpannableStringBuilder span = new SpannableStringBuilder( "  Username" );
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
span.setSpan(new ImageSpan(icon), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
tv.setText(span, BufferType.SPANNABLE);

你可以这样做很简单

 et.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //   loadData(et.getText().toString());
            // et.setCompoundDrawablesWithIntrinsicBounds(, 0, 0, 0);
            et.setCompoundDrawables(null, null, null, null);
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (et.getText().length() == 0)
                et.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_search_category_default, 0, 0, 0);
        }

        public void afterTextChanged(Editable s) {
            if (et.getText().length() == 0)
                et.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_search_category_default, 0, 0, 0);
        }
    });
和布局中的XML

 <EditText
    android:id="@+id/editTextSearch"
    style="@android:style/Widget.Holo.EditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:selectAllOnFocus="false"
    android:hint="Search"
    android:drawableLeft="@android:drawable/ic_search_category_default"
   />

你可以这样做,非常简单

 et.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //   loadData(et.getText().toString());
            // et.setCompoundDrawablesWithIntrinsicBounds(, 0, 0, 0);
            et.setCompoundDrawables(null, null, null, null);
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (et.getText().length() == 0)
                et.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_search_category_default, 0, 0, 0);
        }

        public void afterTextChanged(Editable s) {
            if (et.getText().length() == 0)
                et.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_search_category_default, 0, 0, 0);
        }
    });
和布局中的XML

 <EditText
    android:id="@+id/editTextSearch"
    style="@android:style/Widget.Holo.EditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:selectAllOnFocus="false"
    android:hint="Search"
    android:drawableLeft="@android:drawable/ic_search_category_default"
   />

您可以使用EditText本身,并在EditText的任意一侧使用图像进行提示。请参考以下示例:

    EditText editText = (EditText) findViewById(R.id.edit_text);

    Spannable spannable = new SpannableString("  Mobile Number");
    Drawable drawable = getResources().getDrawable(R.drawable.one);
    drawable.setBounds(50, 0, 0, 0);
    ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
    spannable.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    editText.setHint(spannable);

注意:您可以根据文本的首选方向调整图像上的setBounds()。

您可以使用EditText本身,并在EditText的任意一侧使用图像进行提示。请参考以下示例:

    EditText editText = (EditText) findViewById(R.id.edit_text);

    Spannable spannable = new SpannableString("  Mobile Number");
    Drawable drawable = getResources().getDrawable(R.drawable.one);
    drawable.setBounds(50, 0, 0, 0);
    ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
    spannable.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    editText.setHint(spannable);

注意:您可以根据您对文本的首选方向调整图像上的setBounds()。

看看这个:@Shailesh Limbadiya看看我的答案。看看这个:@Shailesh Limbadiya看看我的答案。您说得对,但我想在上传图像时设置布局,但不知道如何设置?当我这样设置时,文本将设置为中心,但图像将设置为左侧。现在我想设置两者都在中心。您可以使用drawablePadding属性调整填充。如果你想把这个放在中间,那么在中间设置一个空文本,在文本中添加drawableLeft,你还需要在代码中管理该文本。是的,谢谢你的帮助,我确实喜欢这样做,我得到了解决方案。你说对了,但我想在上传图像时设置布局,但不知道怎么做?当我这样设置时,文本将设置为中心,但图像将设置为左侧。现在我想设置两者都在中心。您可以使用drawablePadding属性调整填充。如果你想让它在中间,那么在中间设置一个空文本,并将drawableLeft添加到该文本中,你还需要在代码中管理该文本。是,谢谢你的帮助,我喜欢这样做,我得到了解决方案。你说得对,但我想在上传时使用Costude image设置版面。你说得对,但我想在上传时使用Costude image设置版面