Elektron Kitob ilovasini ishlab chiqish

|
Elektron Kitob ilovasini ishlab chiqish
|
bet | 9/11 | Sana | 23.06.2024 | Hajmi | 2,07 Mb. | | #265279 |
Bog'liq Kurs-Ishi(Ravshan)
Elektron kitob ilovasini android studio dasturida ishlab chiqish hisoblanadi. Endi esa Android studio dasturini ochamiz. U yerdan File bo’limidan New bo’limiga undan keyin New Project ga o’tamiz. Projectga nom beramiz va yuklanishini kutamiz. Yuklanib bo’lgach birinchi navbatda dizayn qismini yaratib olamiz. Mening dizaynim quyidagicha ko’rinishda bo’ldi:
Bu esa shu oyna hosil qilish kodi:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@+id/bookListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
Bunda item lar o’rniga kitoblarimiz nomini qo’yishimiz kerak bo’ladi. Men bu dastur uchun 4ta kichik hikoya qo’shdim. 4ta Item dan iborat bo’ldi va u quyidagicha bo’ldi:
Bu oyna hosil qilish xml kodi esa quyida:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BookReaderActivity">
android:id="@+id/bookTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:padding="16dp"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
android:id="@+id/bookContentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"/>
Bu yerdan biz istalgan hikoyani tanlab ochganimizda boshqa bir oyna hosil bo’ladi va hikoya shu oynada paydo bo’ladi. Masalan quyidagicha:
Bu esa shu kitobni o’qish uchun java kodi:
package com.example.book;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class BookReaderActivity extends AppCompatActivity {
private TextView bookTitleTextView;
private TextView bookContentTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_reader);
bookTitleTextView = findViewById(R.id.bookTitleTextView);
bookContentTextView = findViewById(R.id.bookContentTextView);
String title = getIntent().getStringExtra("title");
String content = getIntent().getStringExtra("content");
bookTitleTextView.setText(title);
bookContentTextView.setText(content);
}
}
Bunisi esa java kodi:
package com.example.ebookapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ListView bookListView;
private String[] bookTitles = {"Kitob 1 ", "Kitob 2", "Kitob 3"};
private String[] bookContents = {
" Kitob 1 matni",
" Kitob 2 matni",
" Kitob 3 matni"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bookListView = findViewById(R.id.bookListView);
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, bookTitles);
bookListView.setAdapter(adapter);
bookListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, android.view.View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, BookReaderActivity.class);
intent.putExtra("title", bookTitles[position]);
intent.putExtra("content", bookContents[position]);
startActivity(intent);
}
});
}
}
Demak bunda bookTitles nomli massiv ichiga biz qo’shmoqchi bo’lgan kitoblarimizni nomini yozib chiqamiz. bookContents nomli massiv ichiga esa shu kitoblarning kontentini yozib chiqishimiz lozim bo’ladi.
|
| |