传递ImageButton值

时间:2015-09-22 21:08:22

标签: android android-fragments android-activity

我是android的新手,并尝试创建一个内部有片段的活动。这个想法是片段有3个ImageButtons。单击的ImageButton会将其图像传递给活动,然后在主活动中的onClick(全屏幕ImageView)之后创建的另一个片段中显示它。问题是我无法弄清楚如何传递信息。我已经阅读过多个问题已经被问过类似于这个问题了,并且浏览了android开发者网站,但是我阅读的越多,它就变得越混乱。我也遇到了f​​indViewById的问题,说它无法解决方法。我不知道它为什么告诉我这个,并尝试了很多东西来解决它,没有任何工作。这是我的代码,任何建议将不胜感激。

主要活动:

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements MainActivityFragment.CanPassData{

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void passData(int value){
        MainActivityFragment fragment = new MainActivityFragment();
        fragment.setData(5);
        getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment).commit();
        //***Taken From Android Developer, I believe this is how you create a new fragment when you pass the data to the main activity****
        if (fragment != null) {
            fragment.getData();
        } else {
            MainActivityFragment newFragment = new MainActivityFragment();
            Bundle args = new Bundle();
            //******No idea what this is calling, pictureChosen and picture are default values taken from the website*******
            args.putInt(MainActivityFragment.pictureChosen, picture);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


            transaction.replace(R.id.fragment, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
        //*****************************************************************************************
    }
    public void processData(){
        MainActivityFragment fragment = (MainActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        int value = fragment.getData();
    }
}

片段:

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivityFragment extends Fragment implements View.OnClickListener {

    private int data;
    private CanPassData parent;

    public interface CanPassData{
        public void passData(int value);
    }

    public MainActivityFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //***findViewById is saying it cannot resolve below
        setContentView(R.layout.fragment_main);
        ImageButton button1 = (ImageButton) findViewById(R.id.imageButton);
        ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
        ImageButton button3 = (ImageButton) findViewById(R.id.imageButton3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        //******************************************************************************************
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    public void onAttach(MainActivity activity){
        parent = (CanPassData) activity;
    }

    public void setData(int value){
        data = value;
    }
    //***onClick also has all findViewByID as cannot resolve method, and i'm not exactly sure what to do within each case, the stuff I have in there was also taken from android developer.
    @Override
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.imageButton:
                ImageView box = (ImageView)findViewByID(R.id.imageButton);
                Drawable name = box.getBackground().getCurrent();
                Intent intent = new Intent (this, newFragment.class);
                intent.putExtra("com.example.coding.assignment2", name);
                startActivity(intent);
                break;
            case R.id.imageButton2:
                box = (ImageView)findViewByID(R.id.imageButton2);
                name = box.getBackground().getCurrent();
                intent = new Intent (this, newFragment.class);
                intent.putExtra("com.example.coding.assignment2", name);
                startActivity(intent);
                break;
            case R.id.imageButton3:
                box = (ImageView)findViewByID(R.id.imageButton3);
                name = box.getBackground().getCurrent();
                intent = new Intent (this, newFragment.class);
                intent.putExtra("com.example.coding.assignment2", name);
                startActivity(intent);
                break;
        }
    }

    public void pushData(){
        parent.passData(data);
    }

    public int getData(){
        return data;
    }
}

很多方法,比如getData和pushData,我不确定如何使用它来获取主要活动的信息,这是在课堂上给我的信息。我想知道是否有一种方法只是在点击时传递drawable本身,然后将该drawable分配给新片段中的ImageView,该片段显示覆盖整个片段。我被告知我需要制作第二个片段xml,但java文件本身应该通过main活动创建。这是对的吗?

1 个答案:

答案 0 :(得分:0)

您无法解析findViewById,因为您必须为View充气才能使用片段findViewById,如下所示:

View v = inflater.inflate(R.layout.fragment_main, parent, false);
ImageButton button1 = (ImageButton) v.findViewById(R.id.imageButton);
ImageButton button2 = (ImageButton) v.findViewById(R.id.imageButton2);
ImageButton button3 = (ImageButton) v.findViewById(R.id.imageButton3);

//call your button.setOnClickListener
return v;

无需致电setContentView()

onClick(View v)中,您还需要致电v.findViewById()

你也可以在一个案例中取消你的变量,如果你想以这种方式使用它们,你应该在任何一个案例之前声明它们。看起来应该是这样的:

public void onClick(View v){
    ImageView box=null;
    Drawable name = null;
    Intent intent = null;
    switch(v.getId()){
       case R.id.imageButton:
            box = (ImageView) v.findViewByID(R.id.imageButton);
            name = box.getBackground().getCurrent();
            intent = new Intent (this, newFragment.class);
            intent.putExtra("com.example.coding.assignment2", name);
            startActivity(intent);
            break;

等等