EditText上的drawableRight(drawableRight of EditText)

2019-09-20 14:44发布

下面是我的布局:

<EditText
android:id="@+id/account_et"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawableRight="@drawable/icon_backall"
android:ems="10"
android:hint="@string/str_md_email"
android:inputType="textEmailAddress"
android:padding="10dp" >
</EditText>

我想说明的drawableRight时EditText上进行聚焦。
并隐藏,而没有焦点。
另外一个是我想设置drawableRight的OnClickListener。
我能怎么做?

Answer 1:

我希望我的样本将帮助您

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:inputType="textEmailAddress" >
    </EditText>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="14dp"
        android:layout_marginTop="43dp"
        android:src="@drawable/ic_launcher"
        android:visibility="invisible" >
    </ImageView>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText1"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

这是在其中进行绘制选项类文件

MainActivity.java

package com.example.doubtedittext;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private EditText etext;
    private ImageView imageView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etext = (EditText) findViewById(R.id.editText1);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
        etext.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                if (hasFocus) {
                    imageView1.setVisibility(View.VISIBLE);

                } else {

                    imageView1.setVisibility(View.INVISIBLE);
                }
            }
        });

        imageView1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        MainActivity.this);

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Click yes to exit!")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // if this button is clicked, close
                                        // current activity
                                        MainActivity.this.finish();
                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // if this button is clicked, just close
                                        // the dialog box and do nothing
                                        dialog.cancel();
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }
        });

    }

}

你需要以上操作成功执行.....



Answer 2:

我建议你添加的ImageView从EditText上分离,并对准他是与顶部对齐他的上面和右对齐和你有完全的控制,所以你可以看不到他,setOnClickListner

    <ImageView android:id="@+id/account_et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/account_et"
        android:layout_alignTop="@+id/account_et"
        android:background="@drawable/icon_backall">
    </ImageView>


Answer 3:

使用View.OnFocusChangeListener 。 把绘制到的EditText当你抓住重点,然后用空可以解决这个问题更换绘制。



Answer 4:

使用像belwo

edittext.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                           if(hasFocus)
                           {}
else {}


            }
        });


文章来源: drawableRight of EditText