为片段中的开始活动制作图片按钮?

时间:2016-09-15 16:23:49

标签: java android xml android-fragments fragment

这是我的片段

public class TabFragment1 extends Fragment {

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

}}

这是我的tab_fragment_1

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/poshzamine"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="تخیلی"
        android:id="@+id/textview"
        android:layout_gravity="right"
        android:textColor="@color/sefid"
        android:textSize="35sp"
        android:layout_marginRight="15dp"/>

</LinearLayout>

我希望在点击开始活动

时编写一个ImageButton代码

2 个答案:

答案 0 :(得分:0)

您应该在xml文件中创建ImageButton视图,然后将onClickListener绑定到它,并在onClick触发器时调用startActivity。

片段类。

public class TabFragment1 extends Fragment {

    ImageButton imageButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_tab_fragment1, container, false);
        imageButton = (ImageButton)view.findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(getActivity(),NewActivity.class);
            }
        });
        return view;}

fragment_tab_fragment1.xml

<?xml version="1.0" encoding="utf-8"?>

android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="sdfsdf"
    android:id="@+id/textview"
    android:layout_gravity="right"

    android:textSize="35sp"
    android:layout_marginRight="15dp"/>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:layout_gravity="center_horizontal" />

祝你好运;)

答案 1 :(得分:0)

使用Intent启动Activity。

 public class TabFragment1 extends Fragment {
    ImageButton imageButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_tab_fragment1, container, false);
        imageButton = (ImageButton)view.findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), NewActivity.class));
            }
        });
        return view;
      }
      }

Xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"  
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=" تخیلی "
        android:id="@+id/textview"
        android:layout_gravity="right"
        android:textSize="35sp"
        android:layout_marginRight="15dp"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"/>

</LinearLayout>