在 AsyncTask 运行 时向 TextView 添加文本

Add Text to TextView while AsyncTask Running

我正在学习AsyncTask。我想在 AyncTask 工作时添加一个像 Loading.... 这样的 TextView。我该怎么做

下面是代码

public class MainActivity extends AppCompatActivity {
private EditText et_bookInput;
private TextView tv_titleText;
private TextView tv_authorText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_bookInput = findViewById(R.id.bookInput);
    tv_titleText = findViewById(R.id.titleText);
    tv_authorText = findViewById(R.id.authorText);

}

public void searchBooks(View view) {
    // Get the name of the book from the input field
    String queryString = et_bookInput.getText().toString();
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if(inputMethodManager != null){
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    new FetchBook(tv_titleText, tv_authorText).execute(queryString);

}

试试下面的代码:

 tv_authorText.setText("");
 tv_titleText.setText(R.string.loding);

我假设您要在 tv_titleText 添加加载文本。

我已经测试了您的代码,您需要在 searchBooks(View view)

中设置 loading 文本
TextView.setText("Loading...")