片段中的意图

时间:2015-06-29 16:42:49

标签: android android-intent layout fragment

我得到片段app.But fragment_home.xml有一个相对布局并有imageviews.I开发了代码片段,但是当我点击imageview时,它不是runninng intents.image视图需要 -

public class HomeFragment extends Fragment {


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

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);


    return rootView;
}

我写这里是为了通过imageviews运行意图。因为我看起来java不支持多个extend.like extends Fragment,Activity实现......等等。

 public class home extends Activity implements View.OnClickListener {

    ImageButton firstModule;
    ImageButton secondModule;
    ImageButton thirdModule;
    ImageButton fourModule;
    ImageButton fiveModule;
    ImageButton sx;
    ImageButton seven;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //init buttons
        firstModule = (ImageButton) findViewById(R.id.imageButton1);
        secondModule = (ImageButton) findViewById(R.id.imageButton2);
        thirdModule = (ImageButton) findViewById(R.id.imageButton3);
        fourModule = (ImageButton) findViewById(R.id.imageButton4);
        fiveModule = (ImageButton) findViewById(R.id.imageButton5);
        sx = (ImageButton) findViewById(R.id.imageButto6);
        seven = (ImageButton) findViewById(R.id.imageButton7);
        firstModule.setOnClickListener(this);
        secondModule.setOnClickListener(this);
        thirdModule.setOnClickListener(this);
        fourModule.setOnClickListener(this);
        fiveModule.setOnClickListener(this);
        sx.setOnClickListener(this);
        seven.setOnClickListener(this);
    }


    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.imageButton1:
                startActivity(new Intent(home.this, MainActivitysld.class));
                break;
            case R.id.imageButton2:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButton3:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButton4:
                startActivity(new Intent(home.this, xxxx.class));
                break;
            case R.id.imageButton5:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButto6:
                startActivity(new Intent(home.this, xxx.class));
                break;
            case R.id.imageButton7:
                startActivity(new Intent(home.this, xxx.class));
                break;
        }

    }

编辑:我添加了fragment_home.xml

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/konu" />


        <ImageButton
            android:id="@+id/imageButton7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/nt" />

        <ImageButton
            android:id="@+id/imageButto6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ders" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/soru" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/cikmis" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ozel" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/strateji" />
    </LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:0)

firstModule = (ImageButton) findViewById(R.id.imageButton1);
firstModule .setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
     startActivity(new Intent(home.this, MainActivitysld.class));
    }});

// for n numbers

答案 1 :(得分:0)

什么是家。这个? 你的片段活动是什么?在startActivity中你应该给出该活动的上下文和类对象。 请交叉检查..并尝试调试,如果您的onclick方法被调用。 如果没有,请设置ImageButton.setclickable(true)。 看看上面的选项是否有效。