تنظیم موقعیت پیغام Toast در اندروید

پنجشنبه 17 دی 1394

دراین مقاله قصد داریم یک پیغام toast را در موقیعت های مختلف نمایش دهیم ، پیغام toast یک پیغام با زمان محدود است که به کاربر جهت اطلاع دادن موضوعی پیغام خواهد داد.

تنظیم موقعیت پیغام Toast در اندروید

معمولا پیغام toast در پایین صفحه نمایش داده می شود ولی در این مقاله می خواهیم این پیغام را در جاهای مختلف صفحه به کاربر نمایش دهیم البته کاربر ابتدا مقدارهایی که می خواهد را وارد می نماید و با این مقدار می تواند دکمه ی مورد نظر را در هر جای صفحه نگاه کند.

ما برای تنظیم کردن موقیعت پیغام toast در گوشی خود در متد آن از 3 پارامتر استفاده می کنیم که متد ها به صورت زیر است:

1-A Gravity constant

2-An x-position offset

3-A y-position offset

این موقعیت را می توان با یک متد setgravity تنظیم نماییم، برای  هر کدام از موقعیت ها قطعه کد زیر لازم است

بالا سمت راست:

Toast toast = Toast.makeText(getApplicationContext(), "TOP RIGHT!", Toast.LENGTH_LONG);  
// Here we can set the Gravity to Top and Right  
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);  
toast.show(); 

بالا سمت چپ:

Toast toast = Toast.makeText(getApplicationContext(), "TOP LEFT!", Toast.LENGTH_LONG);  
// Set the Gravity to Top and Left  
toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);  
toast.show(); 

پایین سمت چپ:

Toast toast= Toast.makeText(getApplicationContext(), "BOTTOM LEFT!", Toast.LENGTH_LONG);  
// Set the Gravity to Bottom and Left  
toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);  
toast.show(); 

پایین سمت راست:

    Toast toast = Toast.makeText(getApplicationContext(), "BOTTOM RIGHT!", Toast.LENGTH_LONG);  
    // Set the Gravity to Bottom and Right  
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);  
    toast.show();  

یک activity جدیدی  ایجاد نمایید و قطعه کد زیر را بنویسید:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.prgguru.example.MainActivity" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="X-Offset" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:gravity="center"
        android:inputType="number" >
 
        <requestFocus />
    </EditText>
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="Y-Offset" />
 
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:gravity="center"
        android:inputType="number" >
 
    </EditText>
 
    <Button
        android:id="@+id/button1"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:onClick="showToastTopLeft"
        android:text="Show Toast (With Top/Left Gravity)" >
    </Button>
 
    <Button
        android:id="@+id/button2"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:onClick="showToastTopRight"
        android:text="Show Toast (With Top/Right Gravity)" >
    </Button>
 
    <Button
        android:id="@+id/button3"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:onClick="showToastBottomLeft"
        android:text="Show Toast (With Bottom/Left Gravity)" >
    </Button>
 
    <Button
        android:id="@+id/button4"
        android:layout_width="500dp"
        android:layout_height="60dp"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:onClick="showToastBottomRight"
        android:text="Show Toast (With Bottom/Right Gravity)" >
    </Button>
 
</RelativeLayout>

داخل کلاس جاوا قطعه کد زیر را بنویسید:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
     
    /**
     * When Button "@+id/button1" is clicked
     * @param v
     */
    public void showToastTopLeft(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Top and Left
            toast.setGravity(Gravity.TOP | Gravity.LEFT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
                    .show();
        }
 
    }
     
    /**
     * When Button "@+id/button2" is clicked
     * @param v
     */
    public void showToastTopRight(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Top and Right
            toast.setGravity(Gravity.TOP | Gravity.RIGHT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
                    .show();
        }
 
    }
     
    /**
     * When Button "@+id/button3" is clicked
     * @param v
     */
    public void showToastBottomLeft(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Bottom and Left
            toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT)
                    .show();
        }
 
    }
     
    /**
     * When Button "@+id/button4" is clicked
     * @param v
     */
    public void showToastBottomRight(View v) {
        EditText e1 = (EditText) findViewById(R.id.editText1);
        EditText e2 = (EditText) findViewById(R.id.editText2);
        if (e1.getText().toString().trim().length() > 0
                && e2.getText().toString().trim().length() > 0) {
            String editText1 = e1.getText().toString();
            String editText2 = e2.getText().toString();
            int xOffset = Integer.parseInt(editText1);
            int yOffset = Integer.parseInt(editText2);
            Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
            // Set the Gravity to Bottom and Right
            toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, xOffset, yOffset);
            toast.show();
        } else {
            Toast.makeText(getApplicationContext(), "Please enter value for X-Offset and/or Y-Offset", Toast.LENGTH_SHORT).show();
        }
 
    }
}

خروجی به صورت زیر خواهد بود:

 

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید