EditText is another important control that the program uses to interact with the user, allowing the user to enter and edit content in the control, and to process the content in the program. EditText application scenarios should be very common, texting, texting, chatting QQ and so on, in these operations, you have to use to EditText. Let's take a look at how to add EditText to the interface, modify the code in the Activity_main.xml as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "Match_parent "
android:orientation= "Vertical" >
......
<edittext android:id= "@+id/edit_text" android:layout_width= "match_parent" android:layout_height= "Wrap_content"
/>
</LinearLayout>
Actually see here, I think you have summed up the use of Android control rules, basically the usage is very similar to the control to define an ID, and then specify the width and height of the control, and then appropriate to add some of the control-specific properties are almost. So using XML to write an interface is not difficult at all, and it can be accomplished without any visual tools. Now rerun the program, the EditText is already displayed on the interface, and we can enter the contents of the inside, 3.5 is shown.
Figure 3.5
Careful you should usually be aware that some of the more user-friendly software will display some suggestive text in the input box, and then once the user has entered any content, these informative words will disappear. This kind of hint is very easy to implement in Android, we don't even need to do any logic control, because the system has been processed for us. Modify the Activity_main.xml as follows:
<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "Match_parent "
android:orientation= "Vertical" >
......
<edittextandroid:id= "@+id/edit_text" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:hint= "Typesomething Here"
/>
</LinearLayout>
This uses the Android:hint property to specify a hint of text, and then rerun the program, the effect
As shown in 3.6.
Figure 3.6
As you can see, a hint of text appears in EditText, and then when we enter anything, the text disappears automatically.
But as the input continues to grow, EditText will be stretched continuously. Because EditText's height specifies wrap_content, it always contains the contents, but when the input is too high, the interface becomes very ugly. We can use the Android:maxlines property to solve this problem and modify the Activity_main.xml as follows:
<linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "Match_parent "
android:orientation= "Vertical" >
......
<edittextandroid:id= "@+id/edit_text" android:layout_width= "match_parent" android:layout_height= "Wrap_content"
Android:hint= "Type something Here"
Android:maxlines= "2"
/>
</LinearLayout>
Here, the maximum number of rows for EditText is specified by Android:maxlines, so that when the input exceeds two lines, the text scrolls upward, and EditText no longer stretches, as shown in 3.7.
Figure 3.7
We can also use a combination of EditText and button to do some functions, such as by clicking the button to get
The content entered in the EditText. Modify the code in the mainactivity as follows:
public class Mainactivity extends Activity implementsonclicklistener {
Private button button;
Private EditText EditText;
@Override
Protected voidoncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);
Button = (button) Findviewbyid (R.id.button);
EditText = (editText) Findviewbyid (R.id.edit_text);
Button.setonclicklistener (this);
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.button:
String inputtext =edittext.gettext (). toString (); Toast.maketext (Mainactivity.this, Inputtext,
Toast.length_short). Show ();
Break
Default
Break
}
}
}
First, through the Findviewbyid () method to get the EditText instance, and then in the button click event Call EditText's GetText () method to obtain the input content, and then call the ToString () method to convert to a string, and finally still the old method, using The toast displays the input content. Rerun the program, enter a piece of content in the EditText, and then click the button, as shown in effect 3.8.
Figure 3.8
Android:edittext controls