Android Studio —— fragment

编程入门 行业动态 更新时间:2024-10-08 04:25:38

<a href=https://www.elefans.com/category/jswz/34/1771384.html style=Android Studio —— fragment"/>

Android Studio —— fragment

效果

源码

CSDN源码下载

代码

     MainActivity.java

package com.example.application;import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {String name[] = new String[]{"人工智能","大数据","区块链","物联网","云计算","VR"};String cont[] = new String[]{"人工智能人工智能人工智能人工智能人工智能人工智能人工智能人工智能人工智能人工智能.","大数据大数据大数据大数据大数据大数据大数据大数据大数据大数据.","区块链区块链区块链区块链区块链区块链区块链区块链区块链区块链.","物联网物联网物联网物联网物联网物联网物联网物联网物联网物联网.","云计算云计算云计算云计算云计算云计算云计算云计算云计算云计算.","VRVRVRVRVRVRVRVRVRVR"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);FragmentManager fragmentManager = getSupportFragmentManager();MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myfragment);TextView TV = myFragment.getView().findViewById(R.id.TV);TV.setText(cont[0]);ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(MainActivity.this,R.layout.item,name);ListView listview = findViewById(R.id.listview);listview.setAdapter(nameAdapter);listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {TV.setText(cont[i]);}});}
}


     activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView" /><ListViewandroid:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="20dp"android:layout_weight="1" /></LinearLayout><fragmentandroid:id="@+id/myfragment"android:name="com.example.application.MyFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3" /></LinearLayout>


     MyFragment.java

package com.example.application;import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;public class MyFragment extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.myfragment,container,false);Button buttonSEND = view.findViewById(R.id.buttonSEND);buttonSEND.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {TextView textView = getActivity().findViewById(R.id.textView);textView.setText("来自Fragment:" + System.currentTimeMillis());}});return view;}
}


     myfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="#D2ED5A"android:gravity="center"android:orientation="vertical"><TextViewandroid:id="@+id/TV"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="0"android:text="..." /><Buttonandroid:id="@+id/buttonSEND"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_weight="0"android:text="发送" />
</LinearLayout>


     item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center"android:text="123"android:textSize="16sp">
</TextView>

关注

笔者 - jxd

微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O

更多推荐

Android Studio —— fragment

本文发布于:2024-02-06 05:41:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1746834.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   Studio   fragment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!