Android tools and simple monitoring events 6.28
1. When developing Android, initially design the function, design the layout, and lay out the interface.
The first interface is the login and registration interface:
The code of activity_main.xml is as follows: Linear layout is used for user login and jump registration. The user can click "Register", the system will pop up a "Jump to the registration page" prompt, and jump to the Second Activity through the Intent.
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myone.MainActivity" android:background="@drawable/bj"> <TextView android:id="@+id/nametext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="username" android:textSize="22dp" android:layout_marginTop="10dp" /> <EditText android:hint="Be careful not to start with a number" android:id="@+id/nvalue" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" > </EditText> <TextView android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" android:textSize="22dp" /> <EditText android:id="@+id/pvalue" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="Please enter a 5-8 digit password" /> <LinearLayout android:layout_marginTop="100px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="login" android:textColor="#000000" android:layout_weight="1" android:layout_marginRight="10dp"/> <Button android:id="@+id/regist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="register" android:textColor="#000000" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
The interface MainActivity.java code is as follows:
package com.example.myone; import javax.security.auth.PrivateCredentialPermission; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.InputFilter.LengthFilter; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button login; private Button regist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); login=(Button)findViewById(R.id.login); regist=(Button)findViewById(R.id.regist); regist.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,Second.class); startActivity(intent); Toast.makeText(MainActivity.this,"Jump to registration page", 100).show(); } }); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i2=new Intent(MainActivity.this,Third.class); startActivity (i2); } }); } }
Here, in order to realize the jump, the registration of Activity must be added to the registry.
You can see that after clicking the registration:
2. Design the registration interface secondfile.xml
Pay special attention to the name, whether it is the first letter or the middle letter, must not be uppercase!
Specific code: The relative layout is used here. Although it is really convenient to drag and drop, the id naming of each component is really awkward. I think the linear layout is clearer.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myone.MainActivity" android:background="@drawable/bj" > <TextView android:id="@+id/wel" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Welcome to register" android:textColor="#000000" android:textSize="45dp" /> <TextView android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/wel" android:layout_below="@+id/wel" android:layout_marginTop="44dp" android:textSize="20dp" android:text="username" /> <EditText android:id="@+id/namevalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/username" android:layout_alignBottom="@+id/username" android:layout_alignRight="@+id/wel" android:ems="10" android:inputType="textPersonName" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/username" android:layout_alignTop="@+id/password" android:text="password" android:textSize="20dp" /> <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/namevalue" android:layout_below="@+id/namevalue" android:layout_marginTop="29dp" android:ems="10" android:inputType="textPassword" > <requestFocus /> </EditText> <TextView android:id="@+id/se" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/TextView01" android:layout_below="@+id/password" android:layout_marginTop="29dp" android:text="gender" android:textSize="20dp" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/se" android:layout_alignBottom="@+id/se" android:layout_alignLeft="@+id/password" android:layout_marginLeft="22dp" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/se" android:layout_marginLeft="44dp" android:layout_toRightOf="@+id/radioButton1" android:text="女" /> <Button android:id="@+id/finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="完成" /> </RelativeLayout>
3. Click Finish, you can jump to the login, which is not shown here.
Second.java
package com.example.myone; import org.apache.http.NameValuePair; import com.example.myone.R.id; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Second extends Activity{ private Button finish; private EditText namevalue; private EditText password; protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.secondfile); finish=(Button)findViewById(R.id.finish); password=(EditText)findViewById(R.id.password); namevalue=(EditText)findViewById(R.id.namevalue); finish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(Second.this, "用户名:"+namevalue.getText().toString()+"密码"+password.getText().toString(),100).show(); Intent i1=new Intent(Second.this,MainActivity.class); startActivity (i1); } }); } }
4. After the user enters the account and password to log in, they can jump to the third Activity, where the main function is to set the picture as the mobile wallpaper.
This part of the function has not been completed today, mainly to read the picture and set it as the desktop.
Due to permission issues, it must be added to the registry
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Third.java
package com.example.myone; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnLongClickListener; import android.widget.ImageView; import android.widget.TextView; public class Third extends Activity{ private TextView text=null; private ImageView image=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.func); image=(ImageView)findViewById(R.id.image); image.setOnLongClickListener(new LongClick()); } private class LongClick implements OnLongClickListener{ @Override public boolean onLongClick(View arg0) { // TODO Auto-generated method stub try { Third.this.clearWallpaper();//Remove the original wallpaper Third.this.setWallpaper(Third.this.image.getResources().openRawResource(R.drawable.back)); } catch (Exception e) { // TODO: handle exception e.printStackTrace (); } return false; } } }
This part is listening for long press, so the event listener is different from the previous one. By using an inner class, it is really convenient to make all the listening events into an interface to call. LongClick() is used here, replacing the previous View.OnLongClickListener().