Android将值传递给另一个活动

时间:2018-08-06 11:18:47

标签: java android android-activity

我有一个带有30个按钮的MainActivity(标识:imageButton1imageButton2 ...)。关于点击事件,我正在启动一个名为KlikNaDugme的新活动:

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    buttons = new ImageButton[30];

    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = (ImageButton) findViewById(R.id.imageButton + i);
        vr = i;
        buttons[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                Intent myIntent = new Intent(HomeActivity.this, KlikNaDugme.class).putExtra("vrijednost", vr);
                startActivity(myIntent);
            }
        });
    }
}

我正在尝试将vr传递给KlikNaDugme活动,该活动被声明为public int

KlikNaDugme活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_klik_na_dugme);

    int x = getIntent().getExtras().getInt("vrijednost");
    System.out.println(x);
}

问题是它总是获得29的值。如何正确传递ID?

4 个答案:

答案 0 :(得分:2)

i的值存储在按钮的标签中:

    buttons = new ImageButton[30];

    for(int i=0; i<buttons.length; i++) {
        {
            buttons[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i + 1), "id", this.getPackageName()));
            buttons[i].setTag(i);

            buttons[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    Intent myIntent = new Intent(HomeActivity.this, KlikNaDugme.class)
                    myIntent.putExtra("vrijednost", Integer.parseInt(view.getTag().toString()));
                    startActivity(myIntent);
                }
            });
        }
    }

答案 1 :(得分:1)

问题出在这里:

buttons[i] = (ImageButton) findViewById(R.id.imageButton + i);

您正在做的是创建一个无效的资源引用,然后根据该引用找到视图,这显然是错误的。请记住,findViewById()需要一个由Android Studio本身生成的资源ID。

解决方案是将所有20-30个视图引用保存在一个数组中,然后使用循环在其上设置点击监听器

答案 2 :(得分:0)

将vr值作为标记添加到按钮,然后使用getTag()方法在onclicklistener内获取vr值

            for(int i=0; i<buttons.length; i++) {
            {
                buttons[i] = (ImageButton) findViewById(R.id.imageButton + i);
                vr = i;
                buttons[i].setTag(vr); // This will se the vr value as tag
                buttons[i].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        int vr = (int)view.getTag()  // this will get the vr value from view
                        Intent myIntent = new Intent(HomeActivity.this,
                                KlikNaDugme.class).putExtra("vrijednost", vr);
                        startActivity(myIntent);
                    }
                });
            }

答案 3 :(得分:0)

R.id.what_ever将为您提供由android studio在R类中生成的整数值,而不是what_ever字符串。您应该使用getResources().getIdentifier()来获取正确的资源ID,并将其传递给findViewById()

请尝试使用此代码。它将为您提供正确的ImageButtons ID:

    buttons = new ImageButton[30];

    for(int i=0; i<buttons.length; i++) {
        {
            String buttonID = "imageButton" + (i+1);
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());

            buttons[i] = (ImageButton) findViewById(resID);
            buttons[i].setTag(i);
            //vr = i;
            buttons[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View args0) {

                    Intent myIntent = new Intent(HomeActivity.this,
                            KlikNaDugme.class).putExtra("vrijednost", (int)args0.getTag());
                    startActivity(myIntent);
                }
            });
        }

享受吧。