Android Studio 1-4 未完待续……
一、Fragmen基础知识
package com.example.day04ex.activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.day04ex.R;
import com.example.day04ex.fragment.BlankFragment;
public class MainActivity extends AppCompatActivity {
private Button btn_show;
private Button btn_AtoF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_show = findViewById(R.id.btn_show);
btn_AtoF = findViewById(R.id.btn_AtoF);
btn_AtoF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
btn_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
BlankFragment fragment = new BlankFragment();
fragmentTransaction.add(R.id.fl_show,fragment);
fragmentTransaction.commit();
}
});
}
}
二、Fragmen高级进阶
1、fragment布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".fragment.BlankFragment">
<TextView
android:id="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorPrimary"
android:textSize="30sp"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
2、fragment代码
package com.example.day04ex.fragment;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.day04ex.R;
import static android.content.ContentValues.TAG;
public class BlankFragment extends Fragment {
private TextView textView;
public BlankFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
textView = inflate.findViewById(R.id.tv_id);
Bundle arguments = getArguments();
if(arguments != null){
String key = arguments.getString("key");
textView.setText(key);
}
return inflate;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "onAttach: 被添加到 活动 中时回调");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate: 只会被调用一次");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated: 所在的 活动 启动完成之后回调");
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG, "onStart: ");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume: ");
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "onPause: ");
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG, "onStop: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG, "onDestroyView: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.i(TAG, "onDetach: ");
}
}
3、activity布局
<?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"
android:gravity="center_horizontal"
tools:context=".AtoF_Activity">
<EditText
android:id="@+id/et_id"
android:hint="写一些东西进来呐"
android:layout_width="300dp"
android:layout_height="wrap_content" />
<!--在activity中定义了点击事件-->
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我就对了"
android:onClick="click"
/>
<!--用来显示内容的,是个容器,里面可以放fragment-->
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linear_layout_id"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
4、activity代码
package com.example.day04ex;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.example.day04ex.fragment.BlankFragment;
import com.example.day04ex.fragment.Show_Fragment;
public class AtoF_Activity extends AppCompatActivity {
private EditText editText;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atof_);
editText = findViewById(R.id.et_id);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.linear_layout_id,new BlankFragment());
fragmentTransaction.commit();
}
public void click(View view) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
String string = editText.getText().toString();
BlankFragment blankFragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putString("key",string);
blankFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.linear_layout_id,blankFragment);
fragmentTransaction.commit();
}
}