EditText中的可点击复合可绘制,在单击时弹出视图

Nou*_*n H 5 android textwatcher android-edittext

我正在使用TextWatcher验证我的输入,我希望我的红色提示图像可以点击,这样我就可以给用户提供视觉反馈,如下图所示.到目前为止,我输入的是当我输入无效数据时出现红色提示图像的编辑文本.我现在想知道如何使这个红色提示可点击并使弹出提示显示如图所示.

EditText提示

这是我到目前为止的代码,

public class MainActivity extends Activity implements TextWatcher{

    EditText username,email = null;
    boolean flag=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      

        username=(EditText) findViewById(R.id.usernametxt);
        email=(EditText) findViewById(R.id.emailtxt);

        username.addTextChangedListener(this);

        username.setOnTouchListener(new View.OnTouchListener(){
        public boolean onTouch(View view, MotionEvent motionEvent) {
            // your code here...
            if(flag)
            {
                email.setText("Error. . .");
            }
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);                
            return false;
            }
        });

        Button loginbtn = (Button) findViewById(R.id.login);
        loginbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Check Validations
                if(username.getText().toString().equalsIgnoreCase(""))
                {
                    username.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.alert, 0);
                    flag=true;
                }
            }
        });

        Button clearbtn = (Button) findViewById(R.id.clear);
        clearbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                username.setText("");
                email.setText("");      
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub      
        username.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        flag=false;
        email.setText("");
    }   
}
Run Code Online (Sandbox Code Playgroud)

Ron*_*nie 5

您必须制作自定义EditText才能获得第一个功能.

解决了这里

点击drawable的弹出视图可以用a来完成 PopupWindow

解决了这里.