1. 程式人生 > >消息提示的三種方式

消息提示的三種方式

toa android width margin pac example sch androi roi

技術分享圖片

1.Toast

效果圖:

技術分享圖片技術分享圖片

(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,"自定義Toast..",Toast.LENGTH_SHORT);
31                 //以中心位置為原點,向右移動200,向下移動200
32                 toast.setGravity(Gravity.CENTER,200,200);
33                 toast.show();
34             }
35         });
36     }
37 }

(3) 使用技術分享圖片

的時候需要在

技術分享圖片

寫入

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

消息提示的三種方式