Android basics - interface development considerations

When doing Android development, you must pay attention that the main thread cannot change the UI interface. If the program crashes when running, if there is no obvious syntax error, please check whether your process conflicts or crashes. If there is a connection with the background, that is, when requesting to send a request to the server, you need to pay special attention, or there is no error, but the network request this code will not be executed. In this case, if there is no problem with the set parameters or other places , but the network connection code is not executed. At this time, you need to check whether there is a conflict between your own processes.

I usually instantiate a Thread class in the main thread, start another thread, and then operate in it, and use the handle to transfer the value after getting the data. The specific usage is as follows:


//定义一个handler,用于线程之间数据的传递,如果定义全局变量的话有局限性,因为进程之间是异步加载,往往会出现,页面显示数据时,还没有捕获到数据,所以建议使用handler进行数据的传递

private Handler myHandler =new Handler(){
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                   .....
                   break;
            }
        }
    };

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        
        ......
        
        new Thread(new Runnable() {
            @Override
            public void run() {
            ......
            Message msg = new Message();
            msg.what = 1;
            msg.obj = ...(传递的值);
            myHandler.sendMesage(msg);
            
        //如果没有要传递的数值,只是需要根据状态进行响应的操作,可以直接用sendEmptyMessageDelayed();第一个参数相当于msg.what,用于状态的判断选择。第二个参数是延迟时间,就是这条语句延迟多长时间执行(毫秒)。
        myHandler.sendEmptyMessageDelayed(1,0);
        
        
        //如果直接在进程中更改UI界面,没有反应的话,试试在语句的前后用Looper.prepare();Looper.loop();包裹一下,例如:
        Looper.prepare();
        Toast.makeText(getContext,"获取数据成功",Toast.LENGTH_SHORT).show();
        Looper.loop();
        
            }
        //如果进程没有反应的话,首先检查是否设置了进程启动,即在实例化类的时候写入.start();启动
        }).start();
    }

android input box prompt text click disappear

Add the attribute android:hint="hint text" to the input box.

android center the text in TexiView

android:gravity="center";

Activity_xml view mode does not display and error message

Error:Error: 'B' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

This kind of error message, mainly the file name in the project, does not conform to the naming convention of the system, just like this error message of mine, there should be no capital letters in the file name, and mine appears The uppercase letter "B", so an error is reported, causing the view mode of the Activity_xml file to not be displayed normally

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324932368&siteId=291194637