Amaliy mashg`ulot topshirig`i Android Studio'da misol bilan DatePicker qo'llanmasi


-qadam: Yangi loyiha yarating va uni DatePickerExample deb nomlang 2-qadam


Download 293 Kb.
bet5/5
Sana28.03.2023
Hajmi293 Kb.
#1301507
1   2   3   4   5
Bog'liq
Amaliy mashgulot topshirigi mobil ilova 110 20 va 111 20 guruhlar

1-qadam: Yangi loyiha yarating va uni DatePickerExample deb nomlang
2-qadam: Res – > layout – > activity_main.xml (yoki) main.xml ni oching va quyidagi kodni qo‘shing:
Ushbu bosqichda biz xml faylini ochamiz va sanani tanlash sanasini ko'rsatish uchun ishlatiladigan tahrirlash matnini ko'rsatish uchun kodni qo'shamiz.
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=".MainActivity">


android:id="@+id/date"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#d4d4d4"
android:hint=" Sana tanlang..."
android:padding="15dp"
android:textColor="#897"
android:textColorHint="#090"
android:textSize="20sp"
android:textStyle=" qalin " />

3-qadam: src -> paket -> MainActvity.java ni oching
Ushbu bosqichda biz MainActivity-ni ochamiz, bu yerda sanani tanlash vositasidan sanani (oyning kuni, oy va yil) ko'rsatish uchun tahrirlash matnini boshlash uchun kodni qo'shamiz va matnni tahrirlash bo'yicha bosish hodisasini amalga oshiramiz, shunda foydalanuvchi matnni tahrirlash tugmachasini bosganida sanani tanlash dialog oynasi. u yerdan paydo bo'lsa, foydalanuvchi oy, oy va yil kunini tanlab sanani belgilashi mumkin, o'rnatilgandan so'ng sana tahrir matnida ko'rsatiladi.
package example.abhiandroid.datepickerexample;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText date;
DatePickerDialog datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// sana tanlashni ishga tushiring va button
date = (EditText) findViewById(R.id.date);
// matnni tahrirlashda bosish hodisasini amalga oshirish
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// taqvim sinfining namunasini oling va taqvimdan joriy sana, oy va yilni oling
final Calendar c = Calendar.getInstance();
int mYil = c.get(Calendar.Yil); // joriy yil
int mOy = c.get(Calendar. OY); // joriy oy
int mkun = c.get(Calendar.kun oy); // joriy kun
// sana tanlash oynasi
datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int yil,
int Yil oyi, int Oy kuni) {
// tahrirlash matnida oy kuni, oy va yil qiymatini belgilang
date.setText(Oy kuni + "/ + (Yil oyi + 1) + "/" + yil);
}
}, mYil, mOy, mkun);
datePickerDialog.show();
} }); }}
Chiqish:
Endi ilovani emulyatorda ishga tushiring va EditText operasiyasida sanani kiriting.

Download 293 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling