如何从不同的活动传递并获取String值?

时间:2013-12-05 07:00:43

标签: android

我现在的场景是,我将开始2个活动来获取一些String值,然后在第3个活动上,我将捕获String值,然后我将比较捕获的String并更改图像源

示例:

  1. 选择程序
  2. 选择日(星期一〜星期五)
  3. 捕获从程序到日期的String传递,并将两者合并并传递给使用ImageView的活动
  4. 我再次编辑了代码。

    第一项活动

    enter code here
    
    public class TestSchedule extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_schedule);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_schedule, menu);
        return true;
    }
    
    public void SSD(View view){
        String programme = "SSD";
        Intent intent = new Intent(TestSchedule.this,TextDay.class);
        intent.putExtra("prog", programme);
        startActivity(intent);
        }
    
    public void EIS(View view){
        String programme = "EIS";
        Intent intent = new Intent(TestSchedule.this,TextDay.class);
        intent.putExtra("prog", programme);
        startActivity(intent);
        }
    
    public void IS(View view){
        String programme = "IS";
        Intent intent = new Intent(TestSchedule.this,TextDay.class);
        intent.putExtra("prog", programme);
        startActivity(intent);
        }
    
    public void IT(View view){
        String programme = "IT";
        Intent intent = new Intent(TestSchedule.this,TextDay.class);
        intent.putExtra("prog", programme);
        startActivity(intent);
        }
    
    }
    

    第二项活动

    public class TextDay extends Activity {
    
    String programme;
    
    private void getData(){
        Bundle extras = getIntent().getExtras();
        if(extras != null){
            programme = extras.getString("prog");
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_day);
        getData();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.text_day, menu);
        return true;
    }
    
    public void monday(View view){
        if(programme.equals("SSD")){
            String programDay = "SSDMon";
            Intent intent = new Intent(TextDay.this, TestShow.class);
            intent.putExtra("progDay", programDay);
            startActivity(intent);
        }
        if(programme.equals("EIS")){
            String programDay = "EISMon";
            Intent intent = new Intent(TextDay.this, TestShow.class);
            intent.putExtra("progDay", programDay);
            startActivity(intent);
        }
        if(programme.equals("IS")){
            String programDay = "ISMon";
            Intent intent = new Intent(TextDay.this, TestShow.class);
            intent.putExtra("progDay", programDay);
            startActivity(intent);
        }
        if(programme.equals("IT")){
            String programDay = "ITMon";
            Intent intent = new Intent(TextDay.this, TestShow.class);
            intent.putExtra("progDay", programDay);
            startActivity(intent);
        }   
        }
    }
    

    将捕获并更改图像源的第3个活动

    enter code here
    
    public class TestShow extends Activity {
    
    ImageView image;
    String table;
    
    private void getData(){
        Bundle extras = getIntent().getExtras();
        if(extras != null){
            table = extras.getString("progDay");
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_show);
        getData();
        image = (ImageView) findViewById(R.id.showTimeTable);
        if(table.equals("SSDMonday")){
            image.setImageResource(R.drawable.ssd_mon);
        }
        if(table.equals("EISMonday")){
            image.setImageResource(R.drawable.eis_mon);
        }
        if(table.equals("ISMonday")){
            image.setImageResource(R.drawable.is_mon);
        }
        if(table.equals("ITMonday")){
            image.setImageResource(R.drawable.it_mon);
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_show, menu);
        return true;
    }
    
    }
    

4 个答案:

答案 0 :(得分:0)

开始新活动时使用捆绑包

Bundle b = new Bundle;
  

b.putString(S,密钥);   intent.putExtra(b)中;   startActivity(意向);

更多: http://www.vogella.com/articles/AndroidIntent/article.html

答案 1 :(得分:0)

喜欢这个

发送活动活动

Intent newActivity1 = new Intent(ActivityA.this, ActivityB.class);
newActivity1.putExtra("choose_programme", position);
startActivity(newActivity1);

接收活动活动

String value = getIntent().getExtras().getString("choose_programme");

答案 2 :(得分:0)

通常,解决方法是将String作为额外的意图,并与此额外字符串一起将意图发送给其他活动

检查此链接以获取有关如何实现此操作的更多详细信息: Pass a String from one Activity to another Activity in Android

评论部分的回答。

答案 3 :(得分:0)

1将图像发送到下一个活动...您可以发送位图,因为它实现了Parcelable

private void sendImage() {
  //Getting the Bitmap from the imageView
  BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
  Bitmap bitmap = drawable.getBitmap();

  Intent intent = new Intent(MainActivity.this, NextActivity.class);
  intent.putExtra("PROGRAMME", "PROGRAMME_VALUE");
  intent.putExtra("DAY", "DAY_VALUE");
  intent.putExtra("IMAGE", bitmap);
  startActivity(intent);
}

2在NextActivity中获取图像

Bundle extras = getIntent().getExtras();
if (extras != null) {
   // Bitmap
   Bitmap image = (Bitmap) extras.get("IMAGE");
   if (image != null) {
     imageView.setImageBitmap(image);
   }
   // String value
   String programmeValue = intent.getString("PROGRAMME");
   String dayValue = intent.getString("DAY");
}