简体   繁体   中英

How to write the toast message inside the edittext onclick listener in android?

I am beginer in android development,

i need to get the toast message after i had enter the text in edittext i had used the following code:

order_dicount_Edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String dis=order_dicount_Edit.getText().toString();
            int disc=Integer.parseInt(dis);
            if(disc>100)
            {

                Toast.makeText(this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();
            }


        }

i am getting the error as follows:

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)

Change this

Toast.makeText(this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();

to

Toast.makeText(ActivityName.this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();

or

Toast.makeText(v.getContext(), "Discount should be less than 100",Toast.LENGTH_SHORT).show();

this does not refer to a valid context. It refers to new View.OnClickListener() which is a annonymous inner class which implements the interface OnClickListener .

Also it is better to have a try catch

try
{
    int disc=Integer.parseInt(dis);

}catch(NumberFormatException e)
{
  e.printStackTrace();
}

instead of this use yourActivity.this or getApplicationContext()

order_dicount_Edit.setOnClickListener(new OnClickListener() 
{
        @Override
        public void onClick(View v) 
       {
             String dis=order_dicount_Edit.getText().toString();
            int disc=Integer.parseInt(dis);
            if(disc>100)
            {
                Toast.makeText( yourActivity.this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();
                //Toast.makeText( getApplicationContext(), "Discount should be less than 100",Toast.LENGTH_SHORT).show();

            }
        }
}

As this always refers to current object, so when you are writting this inside the OnClickListener() interface it refers to OnClickListener() instead of Activity.

You can get same view context where you set onClickListener like

myEditText.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "", Toast.LENGTH_SHORT).show();

            }
        });

try implementing textchange listener instead of click listener

i am showing you a simple example of it.

order_dicount_Edit.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if(order_dicount_Edit.getText().toString().length()>2 && Integer.parseInt(_email.getText().toString())>100){
                Toast.makeText(this, "Discount should be less than 100", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Instead of " this " in Toast.makeText(**this**, "Discount should be less than 100",Toast.LENGTH_SHORT).show(); write yourActivityName .this

Mine worked that way!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM