我使用了android studio附带的标准片段,因为我认为它会简化我的工作,因为我还在学习java。但由于内部代码太多,我已经迷失了,不知道事情是如何运作的。活动我似乎理解基础知识,但在一些研究之后,活动和片段之间存在很大差异
我正在尝试应用我在片段活动中学到的相同原理,但似乎不可能。 它说我想用一个inflater覆盖oncreateview但我似乎不理解背后的概念,为什么我不能使用简单的setcontentview并从imageview和textedit检索内容? 是否有解决方法或者我最好避免碎片?
package com.venomdev.safestorage;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link EncryptFiles.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link EncryptFiles#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class EncryptFiles extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
ImageView UploadFile;
Button bUpload;
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;
private OnFragmentInteractionListener mListener;
/**
* 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 EncryptFiles.
*/
// TODO: Rename and change types and number of parameters
public static EncryptFiles newInstance(String param1, String param2) {
EncryptFiles fragment = new EncryptFiles();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public EncryptFiles() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.UploadFile);
UploadFile = (ImageView) findViewById(R.id.UploadFile);
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
return inflater.inflate(R.layout.fragment_encrypt_files, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) activity;
} else {
throw new RuntimeException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:3)
因为Fragment位于Activity内部,并且不能单独使用。您不能将Fragment用作Activity的完全替代,但您可以将Fragment用作Activity的一部分,也可以在其他活动中重复使用。
您需要调用For Each c As Object In Me.components.Components
If TypeOf c Is Timer Then
CType(c, System.Windows.Forms.Timer).Enabled = False
End If
Next
,因为系统会在片段第一次绘制其用户界面时调用此方法。要为您的片段绘制UI,您必须从此方法返回一个视图,该视图是片段布局的根。如果片段没有提供UI,则可以返回null。
要进一步了解Fragment,您应该阅读official documentation
答案 1 :(得分:2)
是的,就像Niko Yuwono在他的回答中提到的那样。 你的onCreateView必须是这样的。因此,Ui可以使用java代码进行充气,您可以在屏幕上通过设计处理UI点击和用户的交互。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
final View rootview = inflater.inflate(R.layout.fragment_encrypt_files, container, false);
return rootview;
}
您必须在代码中删除Oncreate()方法,因为这不是Activity。