Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

บริบทบน Android คืออะไร


คำจำกัดความ

เป็นบริบทของสถานะปัจจุบันของแอปพลิเคชัน/วัตถุ ช่วยให้วัตถุที่สร้างขึ้นใหม่เข้าใจสิ่งที่เกิดขึ้น โดยทั่วไป คุณจะเรียกมันเพื่อรับข้อมูลเกี่ยวกับส่วนอื่นของโปรแกรมของคุณ (กิจกรรมและแพ็คเกจ/แอปพลิเคชัน) ในโปรแกรมด้านล่าง คุณจะเห็นว่าเราได้สร้าง textView แบบไดนามิกและส่งต่อบริบท บริบทนี้ใช้เพื่อรับข้อมูลเกี่ยวกับสภาพแวดล้อม

ตัวอย่างนี้สาธิตวิธีการแสดงบริบทใน android textView

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/linearLayout"
   android:orientation="vertical"
   android:padding="16sp"
   tools:context=".MainActivity">
</LinearLayout>

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      LinearLayout linearLayout = findViewById(R.id.linearLayout);
      TextView textViewContext = new TextView(getApplicationContext());
      textViewContext.setTextSize(30);
      textViewContext.setText("Application Context");
      textViewContext.setTextColor(Color.RED);
      TextView textViewActivityContext = new TextView(this);
      textViewActivityContext.setTextSize(30);
      textViewActivityContext.setText("Activity Context");
      linearLayout.addView(textViewContext);
      linearLayout.addView(textViewActivityContext);
   }
}

ขั้นตอนที่ 4 − เพิ่มรหัสต่อไปนี้ใน androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก จากนั้นตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ –

บริบทบน Android คืออะไร