安卓开发小功能段

1.隐藏键盘

<pre name="code" class="java"><span style="font-size:24px;">((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(InterviewActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); </span>
 
 

2.进入页面时,页面里有EditText,取消键盘的自动出现

 在AndroidManifest.xml文件里,对应的activity下,加上

android:windowSoftInputMode="stateHidden"

3.listview点击更多按钮时,不让其颜色或背景变化

android:listSelector="#00000000"

4.页面底部栏调用

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(JamendoApplication.getInstance().getPlayerEngineInterface() == null || 
JamendoApplication.getInstance().getPlayerEngineInterface().getPlaylist() == null){
menu.findItem(R.id.player_menu_item).setVisible(false);
} else {
menu.findItem(R.id.player_menu_item).setVisible(true);
}
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.player_menu_item:
PlayerActivity.launch(this, (Playlist)null);
break;
case R.id.about_menu_item:
new AboutDialog(this).show();
break;
case R.id.settings_menu_item:
SettingsActivity.launch(this);
break;
default:
}
return super.onOptionsItemSelected(item);
}

home的menu

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/player_menu_item" android:title="@string/player" android:icon="@drawable/ic_menu_player" />
<item android:id="@+id/settings_menu_item" android:title="@string/settings" android:icon="@drawable/android:ic_menu_preferences" />
<item android:id="@+id/about_menu_item" android:title="@string/about" android:icon="@drawable/android:ic_menu_info_details" />

</menu>

5.外部调用activity

<span style="font-size:24px;">public static void launch(Context c, Playlist playlist){
Intent intent = new Intent(c, PlayerActivity.class);
intent.putExtra("playlist", playlist);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
c.startActivity(intent);
}
这样调用比较方便简洁
6.异步线程的一种写法
private class NewsTask extends AsyncTask<Void, WSError, Album[]> {

@Override
public void onPreExecute() {
mViewFlipper.setDisplayedChild(0);
mProgressBar.setText(R.string.loading_news);
super.onPreExecute();
}

@Override
public Album[] doInBackground(Void... params) {
JamendoGet2Api server = new JamendoGet2ApiImpl();
Album[] albums = null;
try {
albums = server.getPopularAlbumsWeek();
} catch (JSONException e) {
e.printStackTrace();
} catch (WSError e){
publishProgress(e);
}
return albums;
}

@Override
public void onPostExecute(Album[] albums) {

if(albums != null && albums.length > 0){
mViewFlipper.setDisplayedChild(1);
ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this);
albumsAdapter.setList(albums);
mGallery.setAdapter(albumsAdapter);
mGallery.setOnItemClickListener(mGalleryListener);
mGallery.setSelection(albums.length/2, true); // animate to center
} else {
mViewFlipper.setDisplayedChild(2);
mFailureBar.setOnRetryListener(new OnClickListener(){
@Override
public void onClick(View v) {
new NewsTask().execute((Void)null);
}
});
mFailureBar.setText(R.string.connection_fail);
}
super.onPostExecute(albums);
}

@Override
protected void onProgressUpdate(WSError... values) {
Toast.makeText(HomeActivity.this, values[0].getMessage(), Toast.LENGTH_LONG).show();
super.onProgressUpdate(values);
}
}</span>

7.代码方式实现点击事件

<span style="font-size:24px;">mCoverImageView.performClick();</span>

8.异步线程注意点 

使用AsyncTask的容易犯下的错误是在doInBackground方法里面直接对UI元素进行操作。如果需要和UI进行交互,可以配合使用publishProgress和onProgressUpdate。比如

@Override
    protected Object doInbackground(Object... params) 
    {
        ...
        publishProgress("完成了20%");
        ...
        publishProgress("完成了80%");
        ...
        return null;
    }

    protected void onProgressUpdate(Object... progress){
        ...
        textView1.setText((String)progress[0]);
        ...
    }

你可能感兴趣的:(安卓开发小功能段)