如何根据意图开始改变活动内容?

时间:2017-04-20 17:02:45

标签: java android

我有一个名为SI的活动,其中包含GridView,其中包含一些单元,可让您在点击时转到另一个活动。我想知道如何进行相同的活动,只是根据启动它的意图来改变它的TextView。

public class SI extends AppCompatActivity {

    GridView gridView;
    String[] units;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zakladnijednotky);
        gridView = (GridView) findViewById(R.id.gridView1);
        units = getResources().getStringArray(R.array.units);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, units);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                if (gridView.getItemAtPosition(position).toString().contains("amper")) {
                    Intent mintent = new Intent(SI.this, MainActivity.class);
                    startActivity(mintent);
                }
                else if (gridView.getItemAtPosition(position).toString().contains("kilogram")) {
                    Intent kilintent = new Intent(SI.this, MainActivity.class);
                    startActivity(kilintent);
                }
                else if (gridView.getItemAtPosition(position).toString().contains("metre")) {
                    Intent metrintent = new Intent(SI.this, MainActivity.class);
                    startActivity(metrintent);
                }
                else if (gridView.getItemAtPosition(position).toString().contains("second")) {
                    Intent mintent = new Intent(SI.this, MainActivity.class);
                    startActivity(mintent);
                }
                else if (gridView.getItemAtPosition(position).toString().contains("kelvin")) {
                    Intent mintent = new Intent(SI.this, MainActivity.class);
                    startActivity(mintent);
                }
                else if (gridView.getItemAtPosition(position).toString().contains("candela")) {
                    Intent mintent = new Intent(SI.this, MainActivity.class);
                    startActivity(mintent);
                }
                else if (gridView.getItemAtPosition(position).toString().contains("mol")) {
                    Intent mintent = new Intent(SI.this, MainActivity.class);
                    startActivity(mintent);
                }

            }
        });
    }
    ...
}

1 个答案:

答案 0 :(得分:1)

您可以在意图中传递一些数据,并在新活动中读取该数据。

Intent mintent = new Intent(SI.this, MainActivity.class);
String strName = "kilogram";
i.putExtra("KEY_NAME_FOR_STRING_I_NEED", strName);
startActivity(mintent); 

并阅读

Bundle extras = getIntent().getExtras();
newString= extras.getString("KEY_NAME_FOR_STRING_I_NEED");

或另一种方法

newString= getIntent().getStringExtra("KEY_NAME_FOR_STRING_I_NEED");

根据newString中的值更改TextViews

if(newString.equals("kilogram")) {
    //do something...
}