如何发送额外的数据,并通过单击按钮一个接一个

时间:2013-11-23 11:44:09

标签: android android-intent

例如,当我点击but1时,其他活动的textview1必须更改,当我点击but2时,textview2必须更改等。点击我的代码时点击点击第一个所有三个textviews后的3个按钮(一个接一个)正在填充首先应该出现的信息textview. 这是我的第一个活动:

    buttonvis1 = (Button) findViewById(R.id.button1);
    buttonvis2 = (Button) findViewById(R.id.button2);
    buttonvis2.setVisibility(Button.GONE);
    buttonvis1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this,Ostanovki.class);
            intent.putExtra("in1",input_in.getText().toString());
            intent.putExtra("out1", input_out.getText().toString());
            buttonvis2.setVisibility(Button.VISIBLE);
            //startActivity(intent);

        }
    });
    buttonvis3 = (Button) findViewById(R.id.button3);
    buttonvis3.setVisibility(Button.GONE);
    buttonvis2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this,Ostanovki.class);
            intent.putExtra("in2",input_in.getText().toString());
            intent.putExtra("out2", input_out.getText().toString());
            buttonvis3.setVisibility(Button.VISIBLE);
            startActivity(intent);
        }
    });
    buttonvis3.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this,Ostanovki.class);
            intent.putExtra("in3",input_in.getText().toString());
            intent.putExtra("out3", input_out.getText().toString());
            //startActivity(intent);
        }
    });

这是第二个:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ostanovki);
    txtOst1 = (TextView) findViewById(R.id.txtost1);
    txtOst2 = (TextView) findViewById(R.id.txtost2);
    txtOst3 = (TextView) findViewById(R.id.txtost3);


    input1 = getIntent().getExtras().getString("in1");
    output1 = getIntent().getExtras().getString("out1");
    txtOst1.setText("В маршрутку зашло " +input1 +"а вышло " +output1);


    input2 = getIntent().getExtras().getString("in2");      
    output2 = getIntent().getExtras().getString("out2");
    txtOst2.setText("В маршрутку зашло " +input2 +"а вышло " +output2);


    input3 = getIntent().getExtras().getString("in3");      
    output3 = getIntent().getExtras().getString("out3");
    txtOst3.setText("В маршрутку зашло " +input3 +"а вышло " +output3);

我是一个初学者,所以如果我写过傻瓜,不要生气。谢谢;)

3 个答案:

答案 0 :(得分:0)

抱歉,我的错误,现在就像捆绑对象一样接收它:

在activity2中,在onCreate()中,您可以通过检索包(包含调用活动发送的所有消息)并在其上调用getString来获取String消息:

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

答案 1 :(得分:0)

阅读完评论后,我认为使用SharedPreferences可以让您的工作更轻松:

保存您的数据

SharedPreferences pref = getSharedPreferences("any_prefname", MODE_PRIVATE);
Editor editor = pref.edit();
editor.putString("in1",input_in.getText().toString() );
editor.putString("out1",input_out.getText().toString() );
editor.commit();

然后在第二个活动中检索您的数据如下:,

SharedPreferences pref = getSharedPreferences("any_prefname", MODE_PRIVATE);
    input1 = pref.getString("in1", "defaultString");
    output1 = pref.getString("out1", "defaultString");
    txtOst1.setText("В маршрутку зашло " +input1 +"а вышло " +output1);

答案 2 :(得分:0)

如果我理解了您的问题,那么在点击所有问题之后您需要3个按钮,请查看另一个活动中的值,因此您必须将所有三个值放入intent.use以下代码:

Intent intent = new Intent(MainActivity.this,Ostanovki.class);

buttonvis1 = (Button) findViewById(R.id.button1);
buttonvis2 = (Button) findViewById(R.id.button2);
buttonvis2.setVisibility(Button.GONE);
buttonvis1.setOnClickListener(new OnClickListener(){
    public void onClick(View v){

        intent.putExtra("in1",input_in.getText().toString());
        intent.putExtra("out1", input_out.getText().toString());
        buttonvis2.setVisibility(Button.VISIBLE);
        //startActivity(intent);

    }
});
buttonvis3 = (Button) findViewById(R.id.button3);
buttonvis3.setVisibility(Button.GONE);
buttonvis2.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        intent.putExtra("in2",input_in.getText().toString());
        intent.putExtra("out2", input_out.getText().toString());
        buttonvis3.setVisibility(Button.VISIBLE);
        startActivity(intent);
    }
});
buttonvis3.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        intent.putExtra("in3",input_in.getText().toString());
        intent.putExtra("out3", input_out.getText().toString());
        //startActivity(intent);
    }
});

并且在第二个活动中,您必须获取数据,因此请使用以下代码:

   Bundle bundle = getIntent().getExtras();
   String in1= bundle.getString("in1");
   String out1= bundle.getString("out1");

   String in2= bundle.getString("in2");
   String out2= bundle.getString("out2");

   String in3= bundle.getString("in3");
   String out3= bundle.getString("out3");