Three ways of message prompt

1.Toast

Effect picture:

 

 

(1) activity_mian.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.app2.MainActivity"
11     android:orientation="vertical">
12 
13     <Button
14         android:id="@+id/bt1"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="Button" />
18     <Button
19         android:id="@+id/bt2"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="Button2" />
23 </LinearLayout>

2.MainAcivity.java

 1 package com.example.app2;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.Toast;
 9 
10 public class MainActivity extends AppCompatActivity {
11     private Button button1,button2;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         button1=(Button)findViewById(R.id.bt1);
17         button2=(Button)findViewById(R.id.bt2);
18 
19         button1.setOnClickListener(new View.OnClickListener() {
20             @Override
21             public void onClick(View v) {
22                 Toast.makeText(MainActivity.this,"默认Toast..",Toast.LENGTH_SHORT).show();
23                 Toast.makeText(getApplicationContext(),R.string.toast1,Toast.LENGTH_SHORT).show();
 24              }
 25          });
 26  
27          button2.setOnClickListener( new View.OnClickListener() {
 28              @Override
 29              public  void onClick( View v) {
 30                  Toast toast = Toast.makeText(MainActivity. this ,"custom Toast.." ,Toast.LENGTH_SHORT);
 31                  // Take the center position as the origin, move 200 to the right and 200 to the down 
32                  toast. setGravity(Gravity.CENTER,200,200 );
 33                  toast.show();
34             }
35         });
36     }
37 }

(3) use 

   when you need to

write

1 <resources>
2     <string name="app_name">app2</string>
3     <string name="toast1">默认toast111...</string>
4 </resources>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325977462&siteId=291194637