Wednesday, April 3, 2013

Google Android OS -Toast komponentasi



Biror bir habarni ekranga chiqarishda biz Toast va Dialoglardan foydalanishim mumkin.
Ushbu darsda biz Toastni o’rganib chiqamiz.
Yangi proyekt yaratib olamiz, va unga bitta Button komponentasini joylashtiramiz.



Endi Activity faylimizga o’tib quyidagilarni yozamiz:

package com.example.sardor.toast;

/**

 *@author Sardor Dushamov

 */



import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends Activity {



            private Button button1;

            @Override

            protected void onCreate(Bundle savedInstanceState) {

                        super.onCreate(savedInstanceState);

                        setContentView(R.layout.activity_main);

                       

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

                        button1.setOnClickListener(new OnClickListener() {

                                   @Override

                                   public void onClick(View arg0) {

                                               Toast.makeText(MainActivity.this, "Salom bu men Toast!", Toast.LENGTH_LONG).show();

                                   }

                        });

            }

}


activity_main.xml fayli:

<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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="189dp"
        android:text="Ok"
        android:width="90dp" />

</RelativeLayout>



Dasturimizni ishga tushiramiz va OK tugmasini bosamiz.


Ko’rib turganingizdek OK tugmasini bosganimizda tepadagi ramdagidek habar chiqadi.

Toast klassining 1 ta asosiy metodi bor, bu makeText() metido hisoblanadi.

Toast.makeText(MainActivity.this, "Salom bu men Toast!", Toast.LENGTH_LONG).show();

makeText(ApplicationContent,  “Habar”, Vaqt);

Habar – bu yerda biz chiqarmoqchi bulgan habarimizni yozamiz
Vaqt – bu qismida esa qancha vaqt ekranda chiqib turishini ko’rsatamiz, bu yerga son ham yozishimiz mumkin.