如何将此类转换为片段

时间:2016-12-25 20:30:08

标签: android android-fragments

这是我第一次使用片段,我希望将此代码转换为片段 这是我的代码

公共类CONNECTION扩展了AppCompatActivity    {

EditText editText;


private static final String SEND_DATA_URL = "jjj";

Spinner spinner;
String I,Ename,Ecity;
String DEF="NA";

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connection);

    textView=(TextView)findViewById(R.id.textView100);

    spinner=(Spinner)findViewById(R.id.spinner8);
  ArrayAdapter adapter = ArrayAdapter.createFromResource(this,       R.array.connect, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
editText=(EditText)findViewById(R.id.editText22);
    ////////////////////////


}

public void send(View view) {

    CheckInternetConnection cic = new CheckInternetConnection(getApplicationContext());
    Boolean Ch = cic.isConnectingToInternet();
    if (!Ch){
        Toast.makeText(CONNECTION.this, "no conncection ", Toast.LENGTH_SHORT).show();
    }

}


private void openProfile() {
    Intent intent = new Intent(CONNECTION.this, CONNECTION.class);
    startActivity(intent);
}




    }
}}`

我想在这段代码中有一个例子,所以我可以改变其他类的

2 个答案:

答案 0 :(得分:0)

确保 Fragment 位于单独的班级中。要使该类成为 Fragment 类,请在类名后添加 extends Fragment 。请务必覆盖片段类中的 onCreateView() 方法。还有其他问题吗?

答案 1 :(得分:0)

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   return inflater.inflate(R.layout.connection, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);

   textView=(TextView)view.findViewById(R.id.textView100);

  spinner=(Spinner)view.findViewById(R.id.spinner8);
  ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.connect, android.R.layout.simple_spinner_item);
  spinner.setAdapter(adapter);
  editText=(EditText)viewfindViewById(R.id.editText22);
  }   
}

然后将所有剩余的方法和变量从Activity添加到Fragment。在此活动中加载此片段后。