Android:Toast消息的兩種用法

Android:Toast消息的兩種用法

知識點:

  • Toast消息機制
  • LayoutInflater 、inflate()動態加載佈局文件
  • 開發規範

Android Studio

MainActivity.java代碼片.

package com.android.geoquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button mTrueButton;
    private Button mFalseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTrueButton =(Button) findViewById(R.id.true_button);
        mFalseButton = (Button) findViewById(R.id.false_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast mToast = Toast.makeText(MainActivity.this, R.string.right_toast, Toast.LENGTH_SHORT);
                mToast.setGravity(Gravity.CENTER|Gravity.LEFT,0,0);
                mToast.show();
            }
        });
        
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater mInFlater =getLayoutInflater();  ///調用Activity的getLayoutInflater()
                //inflate()動態載入一個res目錄下的xml界面;find.View.By.Id()加載xml文件下的widget控件
                View mLayout = mInFlater.inflate(R.layout.activity_toast,(ViewGroup)findViewById(R.id.custom_toast_container));
                //找到指定位置(即mLayout)的text文本框,並修改其中的content
                TextView mText =(TextView) mLayout.findViewById(R.id.text);
                mText.setText("This is custom toast");
                //Toast消息框機制
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(mLayout);
                toast.show();
            }
        });
    }
}

activity_main.xml代碼片.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/question_content"
        android:padding="20dp"/>

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >

       <Button
           android:id="@+id/true_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/true_button"
           />
       <Button
           android:id="@+id/false_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/false_button"
           />
   </LinearLayout>
</LinearLayout>

activity_toast.xml代碼片.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:background="#DAAA"
    >
    <ImageView android:src="@drawable/ic_launcher_foreground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        />
</LinearLayout> 

修改strings.xml 代碼片.

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_content">canberra is the captital od Australia</string>
    <string name="true_button">TRUE</string>
    <string name="false_button">FALSE</string>
    <string name="right_toast">correct</string>
    <string name="wrong_toast">incorrect</string>
</resources>  

鏈接

谷歌官方文檔:
[1]: https://developer.android.google.cn/guide/topics/ui/notifiers/toasts

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章