The use of explicit jumps
Two simple layouts:
Main
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " > <EditTextAndroid:id= "@+id/et_name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter name:" /> <RadiogroupAndroid:id= "@+id/radiogroup1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <RadioButtonAndroid:id= "@+id/rb_male"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Male" /> <RadioButtonAndroid:id= "@+id/rb_female"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "25DP"Android:text= "female" /> </Radiogroup> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Calculation" /></LinearLayout>
Result
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/tv_name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <TextViewAndroid:id= "@+id/tv_sex"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> <TextViewAndroid:id= "@+id/tv_result"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>
Mainactivity:
PackageOrg.dreamtech.game;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_name; PrivateRadiogroup Rg_group; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_name=(EditText) Findviewbyid (r.id.et_name); Rg_group=(Radiogroup) Findviewbyid (r.id.radiogroup1); } Public voidClick (View v) {String name=Et_name.gettext (). toString (). Trim (); if(Textutils.isempty (name)) {Toast.maketext (), Getapplicationcontext (),"Please enter your name", Toast.length_long). Show (); return; } intRadiobuttonid =Rg_group.getcheckedradiobuttonid (); intSex = 0; Switch(Radiobuttonid) { CaseR.id.rb_male:sex= 1; Break; CaseR.id.rb_female:sex= 2; Break; } if(Sex = = 0) {Toast.maketext (Getapplicationcontext (),"Please select Gender", Toast.length_long). Show (); return; } Intent Intent=NewIntent ( This, Resultactivity.class); Intent.putextra ("Name", name); Intent.putextra ("Sex", Sex); StartActivity (Intent); }}
Resultactivity:
PackageOrg.dreamtech.game;Importjava.io.UnsupportedEncodingException;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classResultactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_result); TextView Tv_name=(TextView) Findviewbyid (r.id.tv_name); TextView Tv_sex=(TextView) Findviewbyid (r.id.tv_sex); TextView Tv_result=(TextView) Findviewbyid (R.id.tv_result); Intent Intent=getintent (); String name= Intent.getstringextra ("name"); byte[] bytes =name.getbytes (); intSex = Intent.getintextra ("Sex", 0); Tv_name.settext (name); Switch(Sex) { Case1: Tv_sex.settext (Male); Try{bytes= Name.getbytes ("GBK"); } Catch(Unsupportedencodingexception e) {} Break; Case2: Tv_sex.settext (Female); Try{bytes= Name.getbytes ("Utf-8"); } Catch(Unsupportedencodingexception e) {} Break; } intTotal = 0; for(byteb:bytes) { intNumber = B & 0xFF; Total+=Number ; } intScore = math.abs (total)% 100; if(Score > 90) {Tv_result.settext ("Very good"); } Else if(Score > 80) {Tv_result.settext ("Nice place to stay"); } Else if(Score > 60) {Tv_result.settext ("Your character is normal"); } Else{Tv_result.settext ("Your character is very poor."); } }}
Android Case: Character Calculator