Thứ Ba, 20 tháng 11, 2018

Android - Tùy chọn được chia sẻ

Học lập trình Android cung cấp nhiều cách lưu trữ dữ liệu của một ứng dụng. Một trong những cách này được gọi là Tùy chọn được chia sẻ. Tùy chọn chia sẻ cho phép bạn lưu và truy xuất dữ liệu dưới dạng cặp khóa, giá trị.

Để sử dụng các tùy chọn chia sẻ, bạn phải gọi phương thức getSharedPreferences () trả về một cá thể SharedPreference trỏ đến tệp chứa các giá trị của các sở thích.

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 

Tham số đầu tiên là khóa và tham số thứ hai là MODE. Ngoài tư nhân có các chế độ khác có sẵn được liệt kê dưới đây

Sr.NoChế độ & mô tả
1MODE_APPEND

Thao tác này sẽ nối thêm các tùy chọn mới với các tùy chọn đã có
2MODE_ENABLE_WRITE_AHEAD_LOGGING

Cờ mở cơ sở dữ liệu. Khi nó được thiết lập, nó sẽ cho phép ghi ghi trước theo mặc định
3MODE_MULTI_PROCESS

Phương thức này sẽ kiểm tra việc sửa đổi các sở thích ngay cả khi cá thể sharedpreference đã được nạp
4MODE_PRIVATE

Bằng cách đặt chế độ này, tệp chỉ có thể được truy cập bằng ứng dụng gọi điện
5MODE_WORLD_READABLE
Chế độ này cho phép ứng dụng khác đọc các tùy chọn
6MODE_WORLD_WRITEABLE

Chế độ này cho phép ứng dụng khác ghi các tùy chọn

Khóa đào tạo lập trình Android có thể lưu một số thứ trong sharedpreferences bằng cách sử dụng lớp SharedPreferences.Editor. Bạn sẽ gọi phương thức chỉnh sửa của cá thể SharedPreference và sẽ nhận nó trong đối tượng trình soạn thảo. Cú pháp của nó là

Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();

Ngoài phương thức putString, có các phương thức có sẵn trong lớp trình soạn thảo cho phép thao tác dữ liệu bên trong các tùy chọn được chia sẻ. Chúng được liệt kê như sau:

Sr. NOChế độ & mô tả
1ứng dụng()

Nó là một phương pháp trừu tượng. Nó sẽ cam kết các thay đổi của bạn trở lại từ trình soạn thảo đối tượng sharedPreference mà bạn đang gọi
2thông thoáng()

Nó sẽ xóa tất cả các giá trị khỏi trình chỉnh sửa
3xóa (khóa chuỗi)

Thao tác này sẽ xóa giá trị có khóa đã được chuyển dưới dạng tham số
4putLong (Khóa chuỗi, giá trị dài)
Nó sẽ tiết kiệm một giá trị dài trong một trình soạn thảo tùy chọn
5putInt (Khóa chuỗi, giá trị int)

Nó sẽ lưu một giá trị số nguyên trong một trình soạn thảo tùy chọn
6putFloat (Khóa chuỗi, giá trị float)

Nó sẽ lưu một giá trị float trong một trình soạn thảo tùy chọn

Thí dụ

Ví dụ này chứng minh việc sử dụng các Sở thích được chia sẻ. Nó hiển thị một màn hình với một số trường văn bản, có giá trị được lưu khi ứng dụng được đóng lại và được đưa trở lại khi nó được mở lại.

Để thử nghiệm với ví dụ này, bạn cần chạy ứng dụng này trên thiết bị thực sau khi phát triển ứng dụng theo các bước bên dưới

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã tiến trình để hiển thị hộp thoại tiến trình quay.
3Sửa đổi tệp res / layout / activity_main.xml để thêm mã XML tương ứng.
4Chạy ứng dụng và chọn một thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả.
Sau đây là nội dung của MainActivity.java đã sửa đổi .
package com.example.sairamkrishna.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   EditText ed1,ed2,ed3;
   Button b1;
 
   public static final String MyPREFERENCES = "MyPrefs" ;
   public static final String Name = "nameKey";
   public static final String Phone = "phoneKey";
   public static final String Email = "emailKey";
 
   SharedPreferences sharedpreferences;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ed1=(EditText)findViewById(R.id.editText);
      ed2=(EditText)findViewById(R.id.editText2);
      ed3=(EditText)findViewById(R.id.editText3);

      b1=(Button)findViewById(R.id.button);
      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String n  = ed1.getText().toString();
            String ph  = ed2.getText().toString();
            String e  = ed3.getText().toString();

            SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putString(Name, n);
            editor.putString(Phone, ph);
            editor.putString(Email, e);
            editor.commit();
            Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
         }
      });
   }

}
Sau đây là nội dung của tệp hoạt động chính đã sửa đổi res / layout / activiy_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Shared Preference "
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="35dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="67dp"
      android:hint="Name"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_below="@+id/editText"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Pass" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText3"
      android:layout_below="@+id/editText2"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Email" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Save"
      android:id="@+id/button"
      android:layout_below="@+id/editText3"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp" />
      
</RelativeLayout>
Sau đây là nội dung của nội dung sửa đổi của tệp res / values ​​/ strings.xml.
<resources>
   <string name="app_name">My Application</string>
</resources>
Sau đây là tệp mặc định nội dung AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>
Hãy thử chạy ứng dụng của bạn. Tôi cho rằng bạn đã kết nối thiết bị Android Mobile thực tế với máy tính của mình.

Để chạy ứng dụng từ Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.

Trước khi bắt đầu ứng dụng của bạn, Android studio sẽ hiển thị cửa sổ sau để chọn tùy chọn mà bạn muốn chạy ứng dụng Android của mình.

Khóa đào tạo lập trình Android

Chọn thiết bị di động của bạn làm tùy chọn và sau đó kiểm tra thiết bị di động của bạn sẽ hiển thị màn hình sau đây 

Bây giờ chỉ cần đưa vào một số văn bản trong lĩnh vực này. Giống như tôi đặt một số tên ngẫu nhiên và các thông tin khác và bấm vào nút lưu.

Bây giờ khi bạn nhấn nút lưu, văn bản sẽ được lưu trong các tùy chọn được chia sẻ. Bây giờ nhấn nút quay lại và thoát khỏi ứng dụng. Bây giờ mở lại và bạn sẽ thấy tất cả văn bản bạn đã viết lại trong ứng dụng của mình.

Không có nhận xét nào:

Đăng nhận xét

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...