9-dars. Androidda xabarlar bilan ishlash.
Biz bundan oldingi darsimizda loglar bilan ishlashni o’rgandik. Bu darsimizda esa androidda xabarlarni GUI ga chiqarishni o’rganamiz. Ya’ni foydalanuvchiga biron bir vazifa bajarilganligi yoki bajarimaganligi haqida ogohlantirish kabidir.
Sevimli eclipseimizni ochamiz va yangi android loyiha yaratamiz.
Application name: AndroidToast
Project Name: AndroidToast
Package Name: uz.projects.androidtoast
Target SDK: API 17 Android 4.2(Jelly Bean)
Create Activity: MainActivity
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="30dp">
<TextView
android:layout_width="wrap_content"
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/tvOut"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp">
</TextView>
<Button
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/btnOk"
android:text="OK"
android:layout_width="100dp">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/btnCancel"
android:text="Cancel"
android:layout_width="100dp">
</Button>
</LinearLayout>
</LinearLayout>
MainActivity.java ga o’tamiz va implements ni onclickListener ni clicl metodini yaratamiz va uni view elementlari bilan ishlashga tayyor qilib qo’ayamiz:
package uz.projects.androidtoast;
import uz.projects.log.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView tvOut;
Button btnOk;
Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnOk:
// OK tugmasi
tvOut.setText("OK tugmasini bosdingiz");
break;
case R.id.btnCancel:
// Cancel tugmasi
tvOut.setText("Cancel tugmasini bosdingiz");
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Tugmalarni bosganimizda TextViewda qaysi tugma bosilganligi haqida habar chiqayotgan edi. Bizga aynan shu hodisani suzub chiquvchi xabar orqaliy amalga oshirishimizga Toast classi yordom beradi. Toast classining makeText metodi orqaliy xabarlarni chiqaramiz. makeText metodining uchta kiruvchi parametlari mavjud.
Toast.makeText(this, "OK tugmasini bosdingiz", Toast.LENGTH_LONG).show();
1. Context-bu aynan qaysi viewda ko’rsatilishidir. Hozirgi oynada ko’rsatish uchun this ni ko’rsatamiz.
2. Text- Xabar mazmuni
3. Duration – xabarning ko’rinish vaqti (Toast.LENGTH_LONG — uzun, Toast.LENGTH_SHORT — qisqa)
Toastni viewda ko’rsatishimiz uchun show() metodidan foydalanamiz. Saqlaymiz va tekshirib ko’ramiz.
Full code:
package uz.projects.androidtoast;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
TextView tvOut;
Button btnOk;
Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnOk:
// OK tugmasi
tvOut.setText("OK tugmasini bosdingiz");
Toast.makeText(this, "OK tugmasini bosdingiz", Toast.LENGTH_LONG).show();
break;
case R.id.btnCancel:
// Cancel tugmasi
tvOut.setText("Cancel tugmasini bosdingiz");
Toast.makeText(this, "Cancel tugmasini bosdingiz", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
8-dars Android. Loglar bilan ishlash.
Maqolani yozishda startandroid.ru saytidan foydalanildi.
Нет комментариев