一月二十八日 安卓开发七
在java类中 添加逻辑代码
package com.example.myapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_query_count; private TextView tu_count; private Handler handler = new Handler() { @Override public void handleMessage(@NonNull Message msg) { if (msg.what == 0) { int count=(Integer)msg.obj; tu_count.setText("数据库中的用户数量为:"+count); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn_query_count = findViewById(R.id.et2); tu_count = findViewById(R.id.et3); btn_query_count.setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.et2) { doQueryCount(); } } private void doQueryCount() { new Thread(new Runnable() { @Override public void run() { int count = data.getUserSize(); Message msg= Message.obtain(); msg.what=0; msg.obj=count; //像主线程发送数据 handler.sendMessage(msg); } }).start(); } }
完成操作