传递多个字符串并在另一个活动上检索它们

时间:2013-12-24 13:32:28

标签: java android android-intent

我需要在点击我的四个按钮的每个按钮时将不同的字符串传递给另一个活动。但每次我得到相同的字符串,无论我点击任何按钮。请看看并帮忙。

MainActivity.java

package com.example.poz;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity 
{

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

        final Button K = (Button) findViewById(R.id.Kids);
        final Button N = (Button) findViewById(R.id.Novice);
        final Button E = (Button) findViewById(R.id.Expert);
        final Button I = (Button) findViewById(R.id.Impossible);
        final Intent activity2 = new Intent (MainActivity.this, PuzzStart.class);
        final Bundle bundle1 = new Bundle();
        final Bundle bundle2 = new Bundle();
        final Bundle bundle3 = new Bundle();
        final Bundle bundle4 = new Bundle();
        bundle1.putString("Kids", "Kids");
        bundle2.putString("Novice", "Novice");
        bundle3.putString("Expert", "Expert");
        bundle4.putString("Impossible", "Impossible");

        K.setOnClickListener(new OnClickListener()
        {
            @Override
        public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            activity2.putExtras(bundle1);
            startActivity(activity2);
        }

        });

        N.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            activity2.putExtras(bundle2);
            startActivity(activity2);
        }

        });

        E.setOnClickListener(new OnClickListener()
        {
        @Override
        public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            activity2.putExtras(bundle3);
            startActivity(activity2);
        }

        });

        I.setOnClickListener(new OnClickListener()
        {
        @Override
        public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            activity2.putExtras(bundle4);
            startActivity(activity2);
        }

        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

PuzzStart.java

package com.example.poz;

import com.example.poz.R.string;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class PuzzStart extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.puzzstart);
    String level = null;
    final TextView lev = (TextView) findViewById (R.id.levelbox);
    final Button shuffle = (Button) findViewById (R.id.shuffle);
    Bundle intent = getIntent().getExtras();
    level = intent.getString("Kids");

    if (level==null)
    {
        level = intent.getString("Novice");         
    }
    if (level==null)
    {
        level = intent.getString("Expert");         
    }
    if (level==null)
    {
        level = intent.getString("Impossible");         
    }

    lev.setText(level);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.puzz_start, menu);
    return true;
}

}

每次,我都得到相同的字符串,即“孩子们”。请告诉我错误和解决方案。

4 个答案:

答案 0 :(得分:2)

改变这个:

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

        final Button K = (Button) findViewById(R.id.Kids);
        final Button N = (Button) findViewById(R.id.Novice);
        final Button E = (Button) findViewById(R.id.Expert);
        final Button I = (Button) findViewById(R.id.Impossible);
        final Intent activity2 = new Intent (MainActivity.this, PuzzStart.class);
        final Bundle bundle1 = new Bundle();

        K.setOnClickListener(new OnClickListener()
        {
            @Override
        public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            bundle1.putString("selectedText", "Kids");

        }

        });

        N.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            bundle1.putString("selectedText", "Novice");

        }

        });

        E.setOnClickListener(new OnClickListener()
        {
        @Override
        public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            bundle1.putString("selectedText", "Expert");

        }

        });

        I.setOnClickListener(new OnClickListener()
        {
        @Override
        public void onClick(View arg0) 
                    {
            // TODO Auto-generated method stub
            bundle1.putString("selectedText", "Impossible");

        }
        activity2.putExtras(bundle1);
        startActivity(activity2);
        });
    }

并检索为:

Bundle intent = getIntent().getExtras();
level = intent.getString("selectedText");
lev.setText(level);

这将是没有重复代码的最佳解决方案。希望有所帮助。

答案 1 :(得分:1)

公共类MainActivity扩展了Activity {

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

    final Button K = (Button) findViewById(R.id.Kids);
    final Button N = (Button) findViewById(R.id.Novice);
    final Button E = (Button) findViewById(R.id.Expert);
    final Button I = (Button) findViewById(R.id.Impossible);
    final Intent activity2 = new Intent (MainActivity.this, PuzzStart.class);
    final Bundle bundle1 = new Bundle();

    K.setOnClickListener(new OnClickListener()
    {
        @Override
    public void onClick(View arg0) 
                {
        // TODO Auto-generated method stub
          bundle1.putString("levels", "Kids");
        activity2.putExtras(bundle1);
        startActivity(activity2);
    }

    });

    N.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
                {
        // TODO Auto-generated method stub
        bundle1.putString("levels", "Novice");
        activity2.putExtras(bundle1);
        startActivity(activity2);
    }

    });

    E.setOnClickListener(new OnClickListener()
    {
    @Override
    public void onClick(View arg0) 
                {
        // TODO Auto-generated method stub
         bundle1.putString("levels", "Expert");
        activity2.putExtras(bundle1);
        startActivity(activity2);
    }

    });

    I.setOnClickListener(new OnClickListener()
    {
    @Override
    public void onClick(View arg0) 
                {
        // TODO Auto-generated method stub
        bundle1.putString("levels", "Impossible");
        activity2.putExtras(bundle1);
        startActivity(activity2);
    }

    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

然后通过

检索
level = intent.getString("levels");
lev.setText(level);

答案 2 :(得分:0)

将字符串放入intent中,如下面的每个按钮的点击监听器:

activity2.putExtra("key", "value");  

并检索为 getIntent().getStringExtra("key")  在最终的活动中。

希望这能解决问题。

答案 3 :(得分:0)

在第一项活动中使用以下常量。

public static final String EXTRA_BUTTON_PRESSED = "EXTRA_BUTTON_PRESSED";
public static final String BUTTON_KIDS = "Kids";
public static final String BUTTON_NOVICE = "Novice";
public static final String BUTTON_EXPERT = "Expert";
public static final String BUTTON_IMPOSSIBLE = "Impossible";

接下来,您不需要使用多个捆绑包。按下按钮时可以准备新的Bundle对象。

K.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        final Intent intent = new Intent (MainActivity.this, PuzzStart.class);
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_BUTTON_PRESSED, BUTTON_KIDS);
        intent.putExtras(bundle);
        startActivity(intent);
    }
});

同样,您可以编写其他按钮点击处理程序。

接下来,在第二个活动中,按如下方式检索值

Bundle bundle = getIntent().getExtras();
String buttonPressed = bundle.getStringExtra(MainActivity.EXTRA_BUTTON_PRESSED);
// Your conditional logic to check which button was pressed.

如果您决定稍后更改任何值,则使用单个位置的常量会有所帮助。

希望这有帮助。