Android 使用“无效”重新绘制无法正常工作
作为最后一年的项目,我们正在android中开发聊天应用程序。我想在用户选择提供的选项时动态更改字体样式。因此使用SharedReferences和invalidate函数重新绘制GUI。下面是我的代码片段 包com.kdr.starAndroid 使用“无效”重新绘制无法正常工作,android,eclipse,Android,Eclipse,作为最后一年的项目,我们正在android中开发聊天应用程序。我想在用户选择提供的选项时动态更改字体样式。因此使用SharedReferences和invalidate函数重新绘制GUI。下面是我的代码片段 包com.kdr.star import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Ty
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences settings=PreferenceManager.getDefaultSharedPreferences(this);
boolean first=settings.getBoolean("first", false);
TextView txt = (TextView) findViewById(R.id.textView1);
if(first)
{
Typeface font=Typeface.MONOSPACE;
txt.setTypeface(font);
txt.Invalidate();
}
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent= new Intent(Main.this,Preference.class) ;
startActivity(intent);
}
});
}
}
这段代码起作用,但部分起作用。选中复选框后,返回时更改不会出现在该实例上。但如果应用程序关闭并再次打开,就会看到更改。但我希望在选中复选框后立即显示更改。即使是“失效”功能也没有帮助。这方面的任何指导都将是非常可贵的。提前感谢因为您将invalidate放在onCreate()中,所以我认为它不会重新绘制更改。
尝试将invalidate()方法放在onClickListener()中,或者创建一个新方法来刷新图像并在侦听器上调用它。尝试postInvalidate()而不是invalidate()
不鼓励在非UI线程中使用invalidate()方法。在我的项目中,我使用invalidate,但有时绘制的结果不会被取消。为什么?我把它作为最后一个代码int onClickListener。即使我复制无效,它发生在不清楚我的形状。那会有什么问题呢?我也会遇到,但invalidate()不是问题所在。其他可能是问题所在的代码,例如无效循环或绘制的内容没有明确指出。