【问题标题】:Pass ImageButton Value传递 ImageButton 值
【发布时间】:2015-09-22 21:08:22
【问题描述】:

我是 android 的新手,正在尝试创建一个包含片段的活动。这个想法是片段有3个ImageButtons。您单击的 ImageButton 会将其图像传递给 Activity,然后在主 Activity 中的 onClick(全屏 ImageView)之后创建的另一个片段中显示它。问题是我不知道如何传递信息。我已经阅读了多个与此类似的问题,并浏览了 android 开发者网站,但我阅读的越多,它变得越混乱。我也遇到了 findViewById 的问题,说它无法解决该方法。我不知道它为什么告诉我这个并且尝试了很多方法来修复它,但没有任何效果。这是我的代码,如有任何建议,我们将不胜感激。

主要活动:

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 等很多方法我不完全确定如何使用来获取主要活动的信息,这是在课堂上给我的信息。我想知道是否有一种方法可以在单击时传递可绘制对象本身,然后将该可绘制对象分配给新片段中的 ImageView,以显示它覆盖整个片段。我被告知我需要制作第二个片段 xml,但 java 文件本身应该通过主要活动创建。这是正确的吗?

【问题讨论】:

  • 哈哈。这可能会令人困惑,因为有多种方法可以做到这一点,那么该怎么做呢?由于您打算从片段 -> 活动 -> 片段开始,您只需创建一个名为(例如)全局的新类文件,您可以在其中定义静态可绘制对象或其他任何内容。 OnClick 事件将图像放入静态全局可绘制对象和 bam!您可以在任何地方使用它,而无需在任何地方编写代码行。由于它是静态的,您需要确保内存泄漏和使用情况,但它是可重用的,因此请重用它。

标签: android android-fragments android-activity


【解决方案1】:

您无法解析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;

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多