你好我是android新手我想問我如何在片段布局中做意圖
任何答案我都會非常感激
這是代碼:
對于 HomeFragment.java
package com.example.splashscreen;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.hotspot2.pps.HomeSp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.constants.ScaleTypes;
import com.denzcoskun.imageslider.models.SlideModel;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment implements View.OnClickListener {
// Initiate
Button button;
View rootView;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@SuppressLint("WrongViewCast")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**Button btnMoveActivity = (Button) rootView.findViewById(R.id.btn1);
btnMoveActivity.setOnClickListener((View.OnClickListener) this);
ImageSlider imageSlider = getActivity().findViewById(R.id.image_slider);
List<SlideModel> slideModel=new ArrayList<>();
slideModel.add(new SlideModel(R.drawable.kucing2, ScaleTypes.FIT));
slideModel.add(new SlideModel(R.drawable.kucing1,ScaleTypes.FIT));
slideModel.add(new SlideModel(R.drawable.kucing2,ScaleTypes.FIT));
imageSlider.setImageList(slideModel,ScaleTypes.FIT);
**/
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_home, container, false);
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
button = (Button) rootView.findViewById(R.id.btn1);
button.setOnClickListener(view1 -> {
switch (view1.getId()){
case R.id.btn1:
Intent moveIntent = new Intent(MainActivity.this,MoveActivity.class); // my button isn't working here
startActivity(moveIntent);
break;
}
});
}
@Override
public void onClick(View view) {
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splashscreen">
<uses-permission android:name="android.permission.INTERNET"/>
<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/Theme.Splashscreen">
<activity
android:name=".Splashscreen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity">
</activity>
</application>
</manifest>
我的按鈕無法執行意圖,我嘗試了幾種我無法理解的方法我想稍后添加影像滑塊,但我更喜歡先使用按鈕
謝謝你的寶貴回答
uj5u.com熱心網友回復:
我不確定這是否適用于您的情況,但如果您想訪問fragment's主機活動并使用它,Intent您可以試試這個
Activity myParentActivity = (MainActivity) getActivity();
Intent moveIntent = new Intent(myParentActivity, MoveActivity.class);
或這個
Activity myParentActivity = (MainActivity) requireActivity();
Intent moveIntent = new Intent(myParentActivity, MoveActivity.class);
把這個放在你的AndroidManifest.xml
<activity android:name=".MoveActivity"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521251.html
標籤:爪哇安卓xml
