我正在尝试从处理程序更改 ActionBar 的背景。最终目标是将 Handler 传递给 AsyncTask,但现在即使从 Thread 调用 Handler.sendMessage() 也会导致奇怪的行为。通过调试器,我可以看到处理程序接收到消息并随后运行 setActionBarBackground() 直到结束。
底部为蓝色笔划的默认 ActionBar 完全从屏幕上消失,并且不会被新的 GradientDrawable 取代。我怀疑背景以某种方式无效。此外,当我再次关注 EditText 时,会出现正确的 GradientDrawable ActionBar 背景。我期望的行为是让背景在 actionDone 上进行简单的更改。
任何关于为什么会发生这种情况的见解将不胜感激!
相关代码:
测试活动.java
public class TestActivity extends RoboSherlockFragmentActivity {
@InjectView(R.id.ET_test) EditText testET;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(MainApp.TAG, "onCreate");
setContentView(R.layout.test_activity);
testET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_DONE) {
String loc = testET.getText().toString();
InputMethodManager mgr = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(testET.getWindowToken(), 0);
Toast.makeText(TestActivity.this, "EditText done!", Toast.LENGTH_SHORT).show();
/*TestQuery tq = new TestQuery(TestActivity.this, mHandler);
tq.execute();*/
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.sendMessage(new Message());
}
}).start();
}
return true;
}
});
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//setActivityColors();
setActionBarBackground();
}
};
private void setActionBarBackground() {
ActionBar ab = getSupportActionBar();
//Drawable d = WidgetUtils.getActionBarDrawable(TestActivity.this, 0xFF00FFFF);
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{0xFFFFFFFF, 0xFF000000});
gd.setCornerRadius(0f);
ab.setBackgroundDrawable(gd);
}
}
test_activity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button"/>
<EditText
android:id="@+id/ET_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:maxLines="1"
android:lines="1"
android:inputType="number"
android:imeOptions="actionDone"
android:nextFocusUp="@id/ET_test"
android:nextFocusLeft="@id/ET_test"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button2"/>
</LinearLayout>