ANDROID OS - Spinner bilan ishlash
Bu darsimizda biz “spinner” bilan tanishib chiqamiz. Agar siz web sohasini yaxshi bilsangiz bu narsani
yaxshi bilasiz, spinner HTML da select va option teglari bilan ishlatiladi. Yani Item lar ro’yhatidan tanlash.
Spinner bilan ishlash juda ham oson.
Yangi project yaratamiz.
Bu proyetk ni nomini men “AndroidSpinner” deb nomladim, siz ham o’zizga yoqqani nom quyishiz mumkin.
layouts papkasini ichidagi main.xml faylini ochamiz va quyidagilarni yozamiz.
main.xml
Hozir biz layoutimizga TextView va Spinner elementlarini joylashtirdik.
Endi AndroidSpinner asosiy Activity classimizni ochamiz va quyidagilarni kiritamiz.
Natija:
Dastur kodini ko'chirib oling.
yaxshi bilasiz, spinner HTML da select va option teglari bilan ishlatiladi. Yani Item lar ro’yhatidan tanlash.
Spinner bilan ishlash juda ham oson.
Yangi project yaratamiz.
Bu proyetk ni nomini men “AndroidSpinner” deb nomladim, siz ham o’zizga yoqqani nom quyishiz mumkin.
layouts papkasini ichidagi main.xml faylini ochamiz va quyidagilarni yozamiz.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Text Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Avtomobillar:"
android:layout_marginBottom="5dp"
/>
<!-- Spinner Element -->
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="Avtomobilni tanlang"
/>
</LinearLayout>
Hozir biz layoutimizga TextView va Spinner elementlarini joylashtirdik.
Endi AndroidSpinner asosiy Activity classimizni ochamiz va quyidagilarni kiritamiz.
package uz.sardordushamov.exmpleSpinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* @author Sardor Dushamov
* @date 25.11.2013
*/
public class AndroidSpinner extends Activity implements AdapterView.OnItemSelectedListener {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
// Spinner elementlarini Listga joylashtiramiz, bu yerda qo'lda joylashtirildi,
// malumotlarni databasedan olib ham joylashtirish mumkin.
List <String> categories = new ArrayList<String>();
categories.add("Tiko");
categories.add("Damas");
categories.add("Nexia");
categories.add("Matiz");
categories.add("Spark");
categories.add("Lacetti");
categories.add("Epica");
categories.add("Captiva");
categories.add("Malibu");
ArrayAdapter <String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
/**
* Bu metodda Spinner element tanlanganda bajariladigan ishlarni yozish mumkin.
* Bu yerda agar biror spinner elementi tanlansa Toast orqali usha tanlangan
* elementni String qiymatini chiqaradi
*/
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(), item+ " ni tanladingiz",
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Natija:
Dastur kodini ko'chirib oling.
Нет комментариев