如何通过其他活动传递和获取字符串值?

韦蒙

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

例子:

  1. 选择程序
  2. 选择日期(周一至周五)
  3. 捕获了从程序到一天的String传递,并将两者结合起来并通过ImageView传递给活动

我再次编辑了代码。

第一次活动

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);
    }   
    }
}

第三次活动将捕获并更改图像源

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;
}

}
可乐190

通常,此问题的解决方案是将字符串作为额外的意图添加到该意图中,并与该额外的字符串一起将意图发送给其他活动

检查此链接以获取有关如何实现此功能的更多详细信息:将字符串从一个Activity传递到Android中的另一个Activity

评论部分的答案。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如果其他字符串值包含yes,则获取字符串的值

来自分类Dev

获取字符串数组的所有其他值

来自分类Dev

从其他类获取字符串

来自分类Dev

使用match()从其他字符串获取字符串

来自分类Dev

如何获取字符串C ++中字符的值?

来自分类Dev

如何在正则表达式中获取字符串并在匹配字符串后删除其他字符串

来自分类Dev

从字符串值获取字符?

来自分类Dev

如何在哈希图中获取字符串和哈希图的混合值

来自分类Dev

Swift如何使用枚举获取字符串值

来自分类Dev

如何获取字符串中的xml节点值

来自分类Dev

如何从JToken获取字符串值

来自分类Dev

如何使用枚举获取字符串值

来自分类Dev

Java:如何获取字符串的枚举值?

来自分类Dev

我如何从json解码并获取字符串值

来自分类Dev

如何获取字符串的ASCII值

来自分类Dev

如何从char *而不是地址获取字符串的值

来自分类Dev

如何获取字符串值而不是整数

来自分类Dev

如何从JSON获取字符串值?

来自分类Dev

如何从流中获取字符串数组值?

来自分类Dev

如何从java列表中获取字符串值

来自分类Dev

如何通过2个活动传递字符串?

来自分类Dev

动态获取字符串值

来自分类Dev

从URL获取字符串值

来自分类Dev

获取字符串的特定值

来自分类Dev

从showOptionDialog获取字符串值

来自分类Dev

从URL获取字符串值

来自分类Dev

获取字符串值,javascript

来自分类Dev

regexp_substr 获取字符串值和管道之间的字符串

来自分类Dev

如何通过CamCard以VCF格式获取字符串名称Android字符串