国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android 下第一個(gè)fragment app 先Java 后Kotlin

這篇具有很好參考價(jià)值的文章主要介紹了Android 下第一個(gè)fragment app 先Java 后Kotlin。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

看著視頻學(xué)習(xí)的,F(xiàn)ragment:3.Fragment使用方法_嗶哩嗶哩_bilibili

程序的運(yùn)行效果是,手機(jī)頁面有2個(gè)fragment,每個(gè)fragment 有一個(gè)text view,一個(gè)按鈕,按一下顯示,'fine,and you?',各自獨(dú)立。

在android studio 下新建一個(gè)工程,類型是?Empty View Activity,本身就有一個(gè)Activity。就有文件MainActivity.java 或者kt,還有一個(gè)layout 文件,activity_main.xml。新建一個(gè)fragment,操作如下圖:

Android 下第一個(gè)fragment app 先Java 后Kotlin,安卓應(yīng)用開發(fā),android,kotlin,開發(fā)語言,java

可以看到左邊有4個(gè)文件:程序文件 BlankFragment.kt,MainActivity.kt (Java版本是Java),布局文件activity.xml,fragment_blank.xml,分別對4個(gè)文件修改,先簡化,再添加點(diǎn)點(diǎn)代碼。然后就測試成功了。

最后文件如下:

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Hello World!" />
    <fragment android:name="com.liwensoft.hellofragment.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:id="@+id/fragment1"
        />
    <fragment android:name="com.liwensoft.hellofragment.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:id="@+id/fragment2"
        />



</LinearLayout>

fragment_blank.xml?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="@string/hello_blank_fragment"
        android:id="@+id/tv1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/button"
        android:text="how are you"/>

</LinearLayout>

應(yīng)用代碼文件MainActivity.kt

package com.liwensoft.hellofragment

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

BlankFragment.kt

package com.liwensoft.hellofragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView


class BlankFragment : Fragment() {

    private lateinit var tv: TextView
    private lateinit var root:View
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        root= inflater.inflate(R.layout.fragment_blank, container, false)
        tv=root.findViewById<TextView>(R.id.tv1)
        val button=root.findViewById<Button>(R.id.button)
        button.setOnClickListener( ){
            tv.setText("fine, and you?")
        }

        return root
    }

}

因?yàn)橐曨l是java 語言,首先完成的是java ,其布局文件一樣,代碼是java的,分別如下:

MainActivity.java

package com.liwensoft.hellofragmentjava;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

FragmentBlank.java

package com.liwensoft.hellofragmentjava;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class BlankFragment extends Fragment {


    private View root;
    private TextView textview;
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(root==null) {
            root = inflater.inflate(R.layout.fragment_blank, container, false);
        }
        textview=root.findViewById(R.id.tv1);
        button=root.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textview.setText("fine,and you?");
            }
        });
        return root;
    }
}

代碼簡單易懂,但我開始轉(zhuǎn)了很多地方,列出方便初學(xué)者上手。文章來源地址http://www.zghlxwxcb.cn/news/detail-701240.html

到了這里,關(guān)于Android 下第一個(gè)fragment app 先Java 后Kotlin的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app(一)

    kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app(一)

    今天我來分享一下如何使用Android Studio開發(fā)一個(gè)天氣預(yù)報(bào)APP。在文中,我們將使用第三方接口獲取實(shí)時(shí)天氣數(shù)據(jù),并顯示在APP界面上。 首先,打開Android Studio并創(chuàng)建一個(gè)新的項(xiàng)目。在創(chuàng)建新項(xiàng)目時(shí),我們需要設(shè)置項(xiàng)目名稱、包名和支持的最低API級別。 為了獲取實(shí)時(shí)天氣數(shù)據(jù),

    2024年02月15日
    瀏覽(29)
  • Android - app內(nèi)部通知通知欄通知Notification (Kotlin)

    Android - app內(nèi)部通知通知欄通知Notification (Kotlin)

    一、簡述 先把通知權(quán)限打開 為什么寫,因?yàn)樵趯W(xué)kotlin剛好順手寫一下,整塊代碼在最后 圖示效果:(圖片來源于網(wǎng)絡(luò)) 1、首先需要一個(gè)NotificationManager對通知進(jìn)行管理,可以通過調(diào)用Context的 getSystemService()方法獲取。getSystemService()方法接收一個(gè)字符串參數(shù)用于確定 獲取系統(tǒng)的

    2024年02月08日
    瀏覽(23)
  • Android-實(shí)現(xiàn)一個(gè)登錄頁面(kotlin)

    Android-實(shí)現(xiàn)一個(gè)登錄頁面(kotlin)

    首先,確保你已經(jīng)安裝了 Android Studio。如果還沒有安裝,請?jiān)L問 Android Studio 官網(wǎng) 下載并安裝。 - 安裝并配置好 Android Studio gradle-wrapper.properties build.gradle(:Project) setting.gradle - 對 Kotlin 語言有基本了解 首先,打開 Android Studio 并創(chuàng)建一個(gè)新項(xiàng)目。選擇 \\\"Empty Activity\\\" 模板,然后為項(xiàng)

    2023年04月25日
    瀏覽(18)
  • kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app (七)使用material design

    kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app (七)使用material design

    對之前的天氣預(yù)報(bào)的app進(jìn)行了優(yōu)化,原先的天氣預(yù)報(bào)程序邏輯是這樣的。 使用text和button組合了一個(gè)輸入城市,并請求openweathermap對應(yīng)數(shù)據(jù),并顯示的功能。 但是搜索城市的時(shí)候,可能會(huì)有錯(cuò)誤,比如大小寫,比如拼寫之類的,所以打算給他升級一下。 目標(biāo): 在搜索的時(shí)候需

    2024年04月27日
    瀏覽(34)
  • kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app(二)增加搜索城市功能

    kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app(二)增加搜索城市功能

    在activity_main.xml里增加輸入框來輸入城市,在輸入款旁邊增加搜索按鈕來進(jìn)行查詢。 然后原來顯示helloworld的TextView用來顯示結(jié)果。 增加搜索按鈕 使用broadcast的方式把收到的天氣信息發(fā)送到界面顯示。 Android的廣播機(jī)制是一種用于在應(yīng)用程序內(nèi)和應(yīng)用程序之間傳遞消息和事件的

    2024年02月14日
    瀏覽(28)
  • [Kotlin]手把手教你寫一個(gè)安卓APP(第一章注冊登錄)

    [Kotlin]手把手教你寫一個(gè)安卓APP(第一章注冊登錄)

    開發(fā)軟件:Android Studio 1.創(chuàng)建項(xiàng)目默認(rèn)選擇Empty Activity ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?點(diǎn)擊Next ?2.生成項(xiàng)目設(shè)置包名選擇開發(fā)語言(這里我用的是kotlin) ?在生成項(xiàng)目后我們要做的就是添加需要的配置打開我們的app目錄下的 buil

    2023年04月23日
    瀏覽(32)
  • kotlin 編寫一個(gè)簡單的天氣預(yù)報(bào)app(六)使用recyclerView顯示forecast內(nèi)容

    要使用RecyclerView顯示天氣預(yù)報(bào)的內(nèi)容 先在grandle里添加recyclerView的引用 創(chuàng)建一個(gè)RecyclerView控件:在布局文件中,添加一個(gè)RecyclerView控件,用于顯示天氣預(yù)報(bào)的列表。 這是一個(gè)包含三個(gè)TextView的LinearLayout布局,用于顯示天氣相關(guān)的數(shù)據(jù)。每個(gè)TextView都有一個(gè)唯一的id,可用于在代

    2024年02月13日
    瀏覽(22)
  • Android開發(fā):基于Kotlin編寫一個(gè)簡易計(jì)算器

    Android開發(fā):基于Kotlin編寫一個(gè)簡易計(jì)算器

    本著程序員“擁抱變化”的思想,最近開始學(xué)Kotlin了。感覺還是得通過實(shí)戰(zhàn)來入門一門新語言,所以打算寫一個(gè)基于Kotlin語言的計(jì)算器,本文對開發(fā)過程以及學(xué)習(xí)Kotlin的一些知識進(jìn)行了記錄。 計(jì)算器的全部源碼已經(jīng)放到了我的Github中,需要的伙伴自?。篊alculator Kotlin中文站:

    2023年04月27日
    瀏覽(25)
  • 解決kotlin寫Android項(xiàng)目編譯報(bào)Execution failed for task ‘:app:kaptDebugKotlin‘.異常

    解決kotlin寫Android項(xiàng)目編譯報(bào)Execution failed for task ‘:app:kaptDebugKotlin‘.異常 遇到的報(bào)錯(cuò)標(biāo)題也是app:kaptDebugKotlin,具體報(bào)錯(cuò)信息不是,也可以試一下這個(gè)方法。 嘗試辦法:將kapt改為annotationProcesser,并同步項(xiàng)目 ,我的項(xiàng)目使用了databinding和arouter,原本是這樣引入的: 上面這樣寫

    2024年02月05日
    瀏覽(36)
  • android計(jì)算器界面布局線性布局跨2行,使用Kotlin高效地開發(fā)Android App(一,GitHub標(biāo)星3.2K

    android計(jì)算器界面布局線性布局跨2行,使用Kotlin高效地開發(fā)Android App(一,GitHub標(biāo)星3.2K

    get(url).placeholder(R.drawable.shape_default_round_bg) .error(R.drawable.shape_default_round_bg) // .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))) .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0)) .into(this) } /** 占位符圓形 */ fun ImageView.loadCircle(url: Drawable) {

    2024年04月11日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包