Android key response and page Jump

Posted by depojones on Tue, 01 Feb 2022 22:50:32 +0100

Several ways of key response

onClick binding function for setting keys in xml

<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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="buttonBeClicked"
        android:text="Key 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="77dp"
        android:onClick="buttonBeClicked"
        android:text="Key 2" />

</RelativeLayout>

Implement the buttonbecclicked function

package com.example.linmu.learn;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void buttonBeClicked(View v){ 
    	switch(v.getId()){ //Get the ID of the button
    		case R.id.button1: // Pass ID
    			System.out.println("As soon as the key is pressed");
    			Toast.makeText(this, "As soon as the key is pressed", 0).show(); //Display in android app for debugging in the future
    			break;
    		case R.id.button2:
    			System.out.println("Key 2 is pressed");
    			Toast.makeText(this, "Key 2 is pressed", 0).show();
    			break;
    	}
    }    
}


The user-defined class implements the interface of key monitoring events

monitor

The event based listening interfaces provided by Android include OnClickListener, OnLongClickListener, OnFocusChangeListener, OnKeyListener, ontuchlistener, OnCreateContextMenuListener, etc.

1) OnClickListener interface: this interface handles click events. In the touch mode, it is a combined action of pressing and lifting on a View, while in the keyboard mode, it is an event of clicking the OK key or pressing the trackball after a View obtains the focus.

2) OnLongClickListener interface: the principle of OnLongClickListener interface is basically the same as the OnClickListener interface above, except that it is the capture interface of View long press event, that is, the event triggered when a View is pressed for a long time.

3) OnFocusChangeListener interface: the OnFocusChangeListener interface is used to handle the event that the focus of the control changes. If the interface is registered, the callback method in the interface will be triggered when a control loses focus or obtains focus.

4) OnKeyListener interface: it is an interface for monitoring the mobile phone keyboard. By registering and monitoring a View, the callback method in the interface will be triggered when the View obtains the focus and has a keyboard event.

5) OnTouchListener interface: it is a monitoring interface used to handle mobile phone screen events. This event will be triggered when touching, pressing, lifting or sliding within the scope of View.

6) OnCreateContextMenuListener interface: it is a listening interface used to handle context menu display events. This method is another way to define and register context menus.

Implemented by OnClickListener

1 bind the local variable button and the button in xml. findViewById
2. Implement the interface class, and implement the onclicked method in the class. Write business code
3 bind the preparations for steps 1 and 2. Make the key press and execute the business code you write. setOnClickListener

package com.example.linmu.learn;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

class MyClieckHandler implements View.OnClickListener{ // Implementation interface class

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	    switch(v.getId()){
	    	case R.id.button1:
	    		System.out.println("As soon as the key is pressed");
//	    		Toast.makeText(this, "once the key is pressed", 0) show();
	    		break;
	    	case R.id.button2:
	    		System.out.println("Key 2 is pressed");
//	    		Toast.makeText(this, "key 2 pressed", 0) show();
	    		break;
	    }
	}
	
	
}

public class MainActivity extends Activity {

	Button btn1;
	Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);//Bind local variable
        btn1.setOnClickListener(new MyClieckHandler());
        btn2.setOnClickListener(new MyClieckHandler());//Make the key press and execute the business code you write 
    
    }

 
}

Anonymous inner class implements key response

Similar to custom classes, only anonymous inner classes are used

package com.example.linmu.learn;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;



public class MainActivity extends Activity {

	Button btn1;
	Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.out.println("As soon as the key is pressed");
	    		Toast.makeText(MainActivity.this, "As soon as the key is pressed", 0).show();
			}
        	
        });
        btn2.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.out.println("Key 2 is pressed");
	    		Toast.makeText(MainActivity.this, "Key 2 is pressed", 0).show();
			}
        	
        });	
       


    }
    
}

Activity implements the click interface

package com.example.linmu.learn;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;



public class MainActivity extends Activity implements View.OnClickListener{

	Button btn1;
	Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
 	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){ //Get the ID of the button
		case R.id.button1: // Pass ID
			System.out.println("As soon as the key is pressed");
			Toast.makeText(this, "As soon as the key is pressed", 0).show(); //Display in android app for debugging in the future
			break;
		case R.id.button2:
			System.out.println("Key 2 is pressed");
			Toast.makeText(this, "Key 2 is pressed", 0).show();
			break;
		}
	}
}

Page Jump

How to jump

Intent intent = new Intent(this, SecondActivity.class);// Set the page to jump to
startActivity(intent); //Jump

How to transfer parameters in jump
1 direct parameter transfer (only one parameter can be transferred)

intent.putExtra("Mydata", "Lin Ruimiao Shuai");

receive

Intent i = this.getIntent();
data = i.getStringExtra("Mydata");

2. Through Bundle (transfer to multiple)

Bundle bundle = new Bundle();
bundle.putString("Mydata", "Lin Ruimiao Shuai");
bundle.putInt("id", 520);
intent.putExtras(bundle);

receive

Intent i = this.getIntent();
Bundle b = i.getExtras();
data = b.getString("Mydata");
id = b.getInt("id");

Concrete implementation

MainActivity.java (Bundle pass parameter)

package com.example.linmu.learn;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void gosecond(View v){
    	Intent intent = new Intent(this, SecondActivity.class);
    	Bundle bundle = new Bundle();
    	bundle.putString("Mydata", "Lin Ruimiao Shuai");
    	bundle.putInt("id", 520);
    	intent.putExtras(bundle);
    	startActivity(intent);
    }
    
}

SecondActivity.java (direct parameter transfer)

package com.example.linmu.learn;

import java.security.PrivateKey;
import java.security.PublicKey;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends Activity {
	
	private int id;
	private String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		Intent i = this.getIntent();
		Bundle b = i.getExtras();
		data = b.getString("Mydata");
		id = b.getInt("id");
		Toast.makeText(this, "Data from the first page:"+data+id, 0).show();
	}

	public void gothird(View v){
		Intent intent = new Intent(this, ThirdActivity.class);
    	
    	intent.putExtra("Mydata", data);
    	startActivity(intent);
	    	
	}
	
}

ThirdActivity.java

package com.example.linmu.learn;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;

public class ThirdActivity extends Activity {
	
	
	private String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_third);
		Intent i = this.getIntent();
		data = i.getStringExtra("Mydata");
		Toast.makeText(this, "Data from the second page:"+data, 0).show();
	}


}

automatic skip

Android thread
Realize the automatic jump effect after several seconds of the page
1. The run method (function) is the "thing" to be done by the thread, which is equivalent to the callback function of Linux C thread
2 start thread

package com.example.linmu.learn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// Go to sleep in the new thread, start another thread, and start a new page in 3 seconds. Do not sleep in the UI thread

		Thread t = new Thread(new Runnable() { //The run method (function) is the "thing" that the thread has to do

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Intent intent = new Intent(MainActivity.this, SecondActivity.class);
				intent.putExtra("Mydata", "Lin Ruimiao Shuai");
				startActivity(intent);
			}
		});// Instantiate a thread
		t.start(); //Start thread

	}

}

Activity lifecycle

Classic life cycle flowchart:


I believe many friends have seen this flow chart and basically understand several processes in the Activity life cycle. Let's talk about these processes.

1. start Activity: the system calls the onCreate method first, then calls the onStart method, finally calls onResume, and Activity enters the running state.

2. The current Activity is overwritten or locked by other activities: the system will call onPause method to suspend the execution of the current Activity.

3. The current Activity returns to the foreground or unlocking screen from the overwritten state: the system will call onResume method to enter the running state again.

4. now Activity is going to the new Activity interface or by pressing the Home key back to the main screen and returning to the background itself: the system will first call the onPause method and then call the onStop method to enter the stagnation state.

5. after the user returns to this Activity: the system will call the onRestart method first, then call the onStart method, and finally call the onResume method to enter the running state again.

6. The current Activity is in the state of being overwritten or invisible in the background, that is, in step 2 and step 4, the system memory is insufficient, kill the current Activity, and then the user returns to the current Activity: call onCreate method, onStart method and onResume method again to enter the running state.

7. the user exits the current Activity: the system calls the onPause method first, then calls the onStop method, and finally calls the onDestory method to end the current Activity.

Topics: Android