如何通过不同的活动显示文本?

时间:2017-11-12 08:54:02

标签: android

当我点击当前活动中的按钮时,比如MainActivity,那么它应该显示您来自Main2Activity中的MainActivity,当我点击Main2Activity上的另一个按钮时,Main3Activity上会弹出另一个活动,然后当我单击一个按钮,它应显示在Main2Activity中,表示它来自Main3Activity。 我只能为MainActivity而不是Main3Activity这样做。 由于我是android的新手,我已经阅读了各种来源,我无法弄清楚如何实现。 这是我的MainActivity

public class MainActivity extends AppCompatActivity {
String d="Main Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void NewmethodtotwoAct(View v){
    Intent i = new Intent(this, Main2Activity.class);
    i.putExtra("Cvalue",d);
    startActivity(i);
 }
}

这是我的Main2Activity

public class Main2Activity extends AppCompatActivity {
String tp,trp1;
int c=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    compare();
    }
void compare() {
    if(c==0) {
        display();
        c++;
    }
    else {
        displaynew();
        c++;
        TextView tet=(TextView)findViewById(R.id.tetview);
        tet.setText("Inelse");
    }
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("MyInt", c);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    int MyInt = savedInstanceState.getInt("MyInt");
    c=MyInt;
}
public void MettoMain(View v){
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}
public void NewmettothreeAct(View v) {
    c=1;
    Intent i = new Intent(this, Main3Activity.class);
    startActivity(i);
}
void display(){
    tp = getIntent().getExtras().getString("Cvalue");
    TextView tet=(TextView)findViewById(R.id.tetview);
    tet.setText(tp);
}
void displaynew() {
    trp1 = getIntent().getExtras().getString("sad");
    TextView tet=(TextView)findViewById(R.id.tetview);
    tet.setText(trp1);

 }
 }

这是我的Main3Activity

public class Main3Activity extends AppCompatActivity {
String dy="Main3Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
}
public void MethtotwoAct(View v){
    Intent i=new Intent(this,Main2Activity.class);
    i.putExtra("sad",dy);
    startActivity(i);
 }
 }

0 个答案:

没有答案