我有不确定的进度条,当我从互联网上读取数据时会显示。为了显示/消失进度条,我使用它的可见性。当我打开进度条选项时,我的应用程序从网络读取数据 15 分钟,当我删除进度条代码时,它会持续 20 秒。有可能它让我的应用程序这么慢吗?
main_activity.xml:
<TableRow
android:gravity="center_horizontal"
android:layout_marginTop="10dip"
android:visibility="gone"
android:id="@+id/progress_bar"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="30dip"
android:minWidth="30dip"
android:maxHeight="30dip"
android:minHeight="30dip"
android:indeterminate="true"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/searchingInProgress"
android:layout_gravity="center|center_horizontal"
/>
</TableRow>
第一个 java 代码片段(这通过执行类 FindSongTask 触发与网络的连接,它继承自 ASyncTask):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button searchButton = (Button) findViewById(R.id.searchButton);
searchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText searchQueryField = (EditText) findViewById(R.id.searchQueryField);
String searchQuery = searchQueryField.getText().toString();
if (searchQuery.length() > 0 && (currentQuery == null || !currentQuery.equals(searchQuery))) {
new FindSongTask().execute(searchQuery);
currentQuery = searchQuery;
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
v.setEnabled(false);
}
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchQueryField.getWindowToken(), 0);
}
});
}
第二个片段(在连接到网络后执行):
instance.findViewById(R.id.searchButton).setEnabled(true);
findViewById(R.id.progress_bar).setVisibility(View.GONE);
super.onPostExecute(result);