Learned two parts today: controls and events
After-school tasks: Design the login interface that needs to fill in the user name and password, and the association of the jump interface after the loginbutton is clicked .
The first interface is done by dragging the controls:
Part of the code is automatically generated in the xml file and edited as needed:
<EditText android:hint="Please enter 6-digit password here" android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView" android:layout_centerHorizontal="true" android:ems="10" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_marginTop="62dp" android:layout_toLeftOf="@+id/editText2" android:text="password" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/editText1" android:layout_alignRight="@+id/textView" android:text="username" /> <EditText android:hint="Please enter username here" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_alignParentTop="true" android:layout_marginTop="106dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginLeft="60dp" android:layout_marginTop="42dp" android:onClick="Loginlis" android:text="login" />
The java class code is as follows:
public class MainActivity extends Activity { private Button login; private EditText account; private TextView password; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Save the state of the Activity setContentView(R.layout.activity_main); login=(Button)super.findViewById(R.id.button1); account=(EditText)super.findViewById(R.id.editText1); password=(TextView)super.findViewById(R.id.editText2); login.setOnClickListener(new OnClickListener() { //OnClickListener listens after clicking login, and there are other methods such as monitoring after long pressing public void onClick(View v) { if(account.getText().toString().equals("123456")){ //equals verification can only be used for string comparison (toString) during class password.setText(account.getText()); Intent t=new Intent(MainActivity.this,SecondActivity.class); //Intent tool connects two activities (ie interface jump) MainActivity.this.startActivity(t); } } }); } }
There is no database at this time, so it is stipulated in the java class that the input password can only be 123456, and the string is judged. When the password is correct, you can jump to another interface after clicking the login button.
In the second interface, only one picture is placed, and the code is relatively simple, so it will not be posted.
Precautions:
Three methods of page jump: binding in xml ; anonymous inner class; judgment in a parent class (such as View v) , different ids correspond to different interface jump results.
It should be especially noted that every time you create a new interface (activity), don't forget to register! ! ! :
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <!-- The entry prompt only needs to be in MainActivity-->> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="SecondActivity" android:label="@string/app_name" > </activity>
I don't know if there is an appropriate solution for the error reported by TT today, but I found a universal method, that is, from the environment construction to the software installation, it is done again.