These days take over to do a remote control Android application, the Android front desk edittext used to enter the IP address, port and other information, found that the use of EditText there is a giant pit one!
I searched the internet for a long while and found that only some people raised this confusion, but did not solve. So write an essay to solve it.
EditText, you can write the content in advance, with the android:text= "..." property. But the problem is, when I change the input, the background is not available! As follows:
XML layout:
< EditText Android:id = "@+id/editip" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " android:text=" http://192.168.1.1 " />
Background access:
1 Public class mainclass{2 Public EditText Editip; 3 Editip = (EditText) Findviewbyid (r.id.editip); 4 String Text = editip.gettext (). TOSTIRNG (); 5 // .... 6 }
Found text and did not get any information entered in the foreground!
Solve:
The problem is: The program executes the above 2, 3, 4 lines, just for a moment, you have not entered the end, it passed! That is, getting the input is not blocked!
The solution is to get it before a blocking behavior, such as when you click on a button, so the program guarantees that the input you get is written in EditText!
That
1 Public classMymainfrmextendsActivity {2 3 PublicEditText Cameraip,controlip,port;4 PublicButton Button_go;5 PublicString Videourl,controlurl,port;6 //Public static String Cameraip;7 /**Called when the activity is first created.*/8 @Override9 Public voidonCreate (Bundle savedinstancestate) {Ten Super. OnCreate (savedinstancestate); One A - Setcontentview (R.LAYOUT.MYMAINFRM); - the -Button_go =(Button) Findviewbyid (r.id.button_go); - - + Button_go.requestfocusfromtouch (); - + AButton_go.setonclicklistener (NewButton.onclicklistener () at { - Public voidOnClick (View v) { - //TODO auto-generated Method Stub - //get here, click Button_go before you are sure to enter the completed - -CAMERAIP =(EditText) Findviewbyid (r.id.editip); inControlip =(EditText) Findviewbyid (R.ID.IP); -Port =(EditText) Findviewbyid (r.id.port); to +Videourl =Cameraip.gettext (). toString (); -Controlurl =Controlip.gettext (). toString (); thePort =Port.gettext (). toString (); * $ Panax Notoginseng //generate a Intent object -Intent Intent =NewIntent (); the //adds a key-value pair to the intent object +Intent.putextra ("Cameraip", Videourl); AIntent.putextra ("Controlurl", Controlurl); theIntent.putextra ("Port", port); + -Intent.putextra ("Is_scale",true); $ //set Intent object to start activity $Intent.setclass (mymainfrm. This, Myvideo.class); - //start another activity with the intent object -Mymainfrm. This. StartActivity (intent); the finish (); -System.exit (0); Wuyi } the }); - Wu}
An easy-to-ignore pit developed by the "Java" Android edittext