4/17 study summary: handler

Today, the layout files and logic codes of the college pages are mainly completed.

The target effect is as follows: Enter the college you want to search in EditView, click the query button to display the score line, report ratio, experience, and join the discussion group for details

 

The problem encountered: When the main content of the TextView is displayed, it cannot be displayed at the beginning (resolved)

Tomorrow ’s goal: to create a drop-down refresh effect

Here is a summary:

一 Handler :

Handler is the core class in the Android SDK that handles asynchronous messages. Its role is to let the child thread update the UI interface by communicating with the UI. 
To sum up, it can also be said: 
1. When the application starts, it will initialize a UI thread 
2.UI Looper is created in the thread, so it is a circular working thread 
. 3. When Looper is created, Looper will create a MessageQueue 

4. Looper in the UI will continue to take messages from the MessageQueue

Two basic principles

Open up a thread. When you need to perform some time-consuming operations, the thread sends a message (Message) to the main thread. This message will enter a queue (MessageQueue), and Looper will take out these messages and give it to the main thread for processing.

Three common methods:

Get Message object:

1.Message messag = new Message (); Such a method needs to open up a space and occupy memory

2.Message nessage = handler.obtainMessage (); Take a Message from the current thread, handler is a Handler object

Some common attributes of Message:

message.what: is a user-defined message code so that the recipient can identify what the message is about

message.obj: If you need to store the object type, you can use this property

message.arg1 (arg2) If you need to store some integers, you can use these two attributes directly

Some common methods of Handler:

handler.obtainMessage (); Get a Message

handler.sendMessage (message): send a message

handleMessage (message): The message passed from the last method is processed by this method

Three main codes

package com.example.kaoyanpai.ui.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.kaoyanpai.DBService;
import com.example.kaoyanpai.R;
import com.example.kaoyanpai.school;

import java.util.List;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private Button button;
    private EditText editText;
    private TextView textView;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.i("789","789");
            switch (msg.what){
                case 0x11:
                    String s = (String) msg.obj;
                    textView.setText(s);
                    Log.i("message","123");
                    break;
                case 0x12:
                    String ss = (String) msg.obj;
                    textView.setText(ss);
                    break;
            }

        }
    };
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        onStart(root);
        return root;

    }


    protected void onStart(View v){
          button=(Button)v.findViewById(R.id.button3);
          editText=(EditText)v.findViewById(R.id.schoolname);
          textView=(TextView)v.findViewById(R.id.show);
          button.setOnClickListener(new View.OnClickListener(){

              @Override
              public  void onClick (View v) {
                   // Create a thread 
                  new Thread ( new Runnable () {
                      @Override
                      public void run() {
                          Looper.prepare();
                  
                          Message message=handler.obtainMessage();
                          message.what=0x11;
                          message.obj="chenggong";
                          handler.sendMessage(message);
                      }
                  }).start();
              }
          });
    }

}

 

Guess you like

Origin www.cnblogs.com/wangzhaojun1670/p/12722091.html