Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidで1つのフラグメントから別のフラグメントにデータを渡す方法は?


この例は、Androidで1つのフラグメントから別のフラグメントにデータを渡す方法について示しています

ステップ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"
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <LinearLayout
      android:id = "@+id/linearlayout01"
      android:layout_width = "fill_parent"
      android:layout_height = "fill_parent"
      android:background = "#ccc"
      android:layout_weight = "1"
      android:orientation = "vertical">
      <fragment android:name = "com.example.myapplication.FirstFragment"
         android:id = "@+id/frag_1"
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent" />
   </LinearLayout>
   <LinearLayout
      android:id = "@+id/linearlayout02"
      android:layout_width = "fill_parent"
      android:layout_height = "fill_parent"
      android:layout_weight = "1"
      android:background = "#eee"
      android:orientation = "vertical">
      <fragment android:name = "com.example.myapplication.SecondFragment"
         android:id = "@+id/frag_2"
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent" />
   </LinearLayout>
</LinearLayout>

上記のコードでは、2つのフラグメント間でデータを渡すためにフラグメントを取得しています。

ステップ3 −次のコードをsrc/MainActivity.javaに追加します

<?xml version = "1.0" encoding = "utf-8"?>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements OnButtonPressListener {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   @Override
   public void onButtonPressed(String msg) {
      // TODO Auto-generated method stub
      SecondFragment Obj = (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.frag_2);
      Obj.onFragmentInteraction(msg);
   }
}

ステップ4 −次のコードをsrc / FirstFragment.java

に追加します
<?xml version = "1.0" encoding = "utf-8"?>
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FirstFragment extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
      ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
      init(root);
      return root;
   }
   OnButtonPressListener buttonListener;
   @Override
   public void onAttach(Activity activity) {
      super.onAttach(activity);
      try {
         buttonListener = (OnButtonPressListener) getActivity();
      } catch (ClassCastException e) {
         throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
      }
   }
   void init(ViewGroup root) {
      TextView but = (TextView)root.findViewById(R.id.text);
      but.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // TODO Auto-generated method stub
            buttonListener.onButtonPressed("Message From First Fragment");
         }
      });
   }
}

ステップ5 −次のコードをsrc / SecondFragment.java

に追加します
<?xml version = "1.0" encoding = "utf-8"?>
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment {
   TextView textView;
   View view;
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view = inflater.inflate(R.layout.fragment, container, false);
      return view;
   }
   public void onFragmentInteraction(String uri) {
      Log.d("sai",uri);
      textView = view.findViewById(R.id.text);
      textView.setText(uri);
   }
}

ステップ6 −次のコードをres / layout/fragment.xmlに追加します。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout
   xmlns:android = "https://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:text = "Click"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

ステップ7 −次のコードをsrc/OnButtonPressListener.javaに追加します。

<?xml version = "1.0" encoding = "utf-8"?>
public interface OnButtonPressListener {
   public void onButtonPressed(String msg);
}

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–

Androidで1つのフラグメントから別のフラグメントにデータを渡す方法は?

次に、最初のフラグメントテキストビューをクリックすると、以下に示すような結果が表示されます–

Androidで1つのフラグメントから別のフラグメントにデータを渡す方法は?


  1. Androidで1つのアクティビティから別のアクティビティに画像を渡す方法は?

    この例は、Androidで1つのアクティビティから画像を渡す方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://

  2. Androidで1つのフラグメントから別のフラグメントにデータを送信するにはどうすればよいですか?

    この例は、SDカード上のフォルダー/ファイルにAndroidのアクセス許可を設定して書き込みできるようにする方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <androidx.coordinat