开发者

android switchstatement question

I was wondering, how would I make a switch statement, that when that certain case was triggered开发者_如何学Python, it would open a new screen with text. Would I use an intent? And if so, which one?

Thank you for your help in advance.


When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:

Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);

this will explicitly attempt to open a new Activity with the class name MySecondActivity

to pass a String of text from one Activity to another in this way, you can add it to the intent.

String someValue = "Some Value";
intent.putExtra("Some Key", someValue);

and in the code of your other Activity, you can get at this string via the Intent

getIntent().getStringExtra("Some Key");

Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.


Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.

The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜