Toast Introduction (learn Lin Guo teacher works)

Toast is a good way to remind Android system provides.

It can be some of the short message is displayed on the screen, and soon may disappear, will not occupy the screen.

Use Toast, Toast we first need to set a trigger point to an example following example:

Button button1=(Button) findViewById(R.id.button_1);

button1.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

   Toast.makeTex(FirstActivity.this,"hello",Toast.LENGTH_SHORT).show();

   }

   });

)

In activity, obtained by the findViewById () method defined element in the layout file to obtain View objects, by forcing, into Button object.

In the f method can register a listener for the button by calling setOnClickListener (), click on the button will come true setOnClickListener () method inside the onClick () method.

As for the use of Toast, Toast we create an object via makeText () method, by show () method is displayed.

makeText () method requires passing three parameters, the first parameter is the Context (Toast requirements of context), remember well, the activity itself is a Context object, so we write directly on top of the FirstActivity.this, the second argument is content to be displayed, the third parameter is displayed Toast long.

This is me after reading Guo Lin teacher's "second line", in order to better memory write, but also the desire to help you to read this blog.

发布了37 篇原创文章 · 获赞 10 · 访问量 1万+

Guess you like

Origin blog.csdn.net/OneLinee/article/details/77460475