Click events and dynamic additions to the Android Studio rookie combat Project

Source: Internet
Author: User

Original interface: Login failed: Login Successful: Add control dynamically:

The layout is as follows: (special declaration of the last empty LinearLayout, this is for the later dynamic add event)

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns: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"
android:background= "@color/chongse"
Android:padding= "0DP"
tools:context= "Com.example.administrator.pro2_1.MainActivity" >
<textview
android:layout_margintop= "5DP"
Android:textsize= "40SP"
Android:id= "@+id/text1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/text1"/>

<edittext
android:layout_marginleft= "10DP"
Android:textsize= "30SP"
android:layout_margintop= "10DP"
Android:id= "@+id/et1"
android:hint= "@string/hint1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
/>
<edittext
android:layout_marginleft= "10DP"
Android:textsize= "30SP"
android:layout_margintop= "10DP"
Android:id= "@+id/et2"
android:hint= "@string/hint2"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
/>
<imagebutton
Android:id= "@+id/img1"
android:src= "@drawable/state1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<button
Android:id= "@+id/bt"
android:text= "@string/BT"
android:layout_marginleft= "10DP"
Android:layout_width= "80DP"
android:layout_height= "50DP"/>
<linearlayout
android:orientation= "vertical" android:layout_width= "match_parent"
Android:id= "@+id/add"
android:layout_height= "Match_parent" >
</LinearLayout>
</LinearLayout>

To implement a listener event:
Package com.example.administrator.pro2_1;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ImageButton;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
public class Mainactivity extends appcompatactivity{
Define a series of variables
Private EditText Et1;
Private EditText Et2;
Private ImageButton IMG_BT;
Private button button;
Private TextView add;
LinearLayout LinearLayout;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Instantiating a control
Initview ();
/* Implement three listener events respectively:
1. Press the tablet button, if the user name and password is correct, then login successfully, hide two EditView button
2. Press the reset button to clear the contents of the EditView
3. Press and hold the picture button to dynamically add a TextView
*/
Img_bt.setonclicklistener (New Imageonclicklistener ());
Button.setonclicklistener (New Buttononclicklistener ());
Img_bt.setonlongclicklistener (New Longtimeonclicklistener ());
}

private void Initview () {
et1= (EditText) Findviewbyid (R.ID.ET1);
Et2= (EditText) Findviewbyid (R.id.et2);
img_bt= (ImageButton) Findviewbyid (R.ID.IMG1);
button= (Button) Findviewbyid (R.ID.BT);
linearlayout= (LinearLayout) Findviewbyid (R.id.add);
}

Class Imageonclicklistener implements view.onclicklistener{
@Override
public void OnClick (View v) {
Get Data
String str1,str2;
String user=getresources (). getString (R.string.user);
String psw=getresources (). getString (R.STRING.PSW);
String error=getresources (). getString (R.string.error);
Str1=et1.gettext (). toString ();
Str2=et2.gettext (). toString ();
When the user name field is empty, the focus is placed on the user name bar and returned directly, the password bar similarly
if (Str1.equals ("")) {
Et1.requestfocus ();
Return
}else if (Str2.equals ("")) {
Et2.requestfocus ();
Return
}
After the input is complete, determine if the user name and password are correct
if (str1.equals (user) &&str2.equals (PSW)) {
Correct, hide two controls and change the picture
Et1.setvisibility (View.gone);
Et2.setvisibility (View.gone);
Img_bt.setimagedrawable (Getresources (). getdrawable (R.drawable.state2));
}else{
If wrong, empty the password bar and add the error prompt
Et2.requestfocus ();
Et2.settext ("");
Et2.sethint (Error);
}
}
}

Class Buttononclicklistener implements view.onclicklistener{
@Override
public void OnClick (View v) {
String str=getresources (). getString (R.STRING.HINT1);
Et1.settext ("");
Et2.settext ("");
Et2.sethint (str);
Et1.requestfocus ();
}
}

Class Longtimeonclicklistener implements view.onlongclicklistener{
@Override
public boolean Onlongclick (View v) {
String str=getresources (). getString (R.string.add_text);
Define a TextView in the mainactivity
Add=new TextView (mainactivity.this);
Add.settext (str);
Add.settextsize (25);
Add to the LinearLayout button that was previously laid out as empty
Linearlayout.addview (add);
return true;
}
}

}

Click events and dynamic additions to the Android Studio rookie combat Project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.