2012-02-07 51 views
0

我們在android中做聊天應用程序作爲最後一年的項目,並且android.I想要動態更改字體樣式,因爲用戶選擇提供的選項。所以使用sharedpreferences和invalidate函數來重繪GUI 。以下是我的代碼片段 package com.kdr.star;重繪無法正常使用invalidate

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); 
} 
    }); 
    } 
    } 

此代碼有效,但部分。當選中該複選框時,更改在返回時不會出現在該實例中。但是如果應用程序關閉並再次打開,則會看到更改。但我希望在選中複選框後立即顯示更改。即使無效功能也沒有幫助。有沒有其他方法可以實現它。任何有關這方面的指導都將非常可觀。在此先感謝

回答

0

由於您將無效放入onCreate(),我不認爲它會重繪更改。
嘗試在onClickListener()中放置invalidate()方法或創建一個新方法來刷新圖像並在偵聽器上調用它。

+0

在我的項目中,我使用invalidate,但有時繪製不會消失。爲什麼?我把它作爲onClickListener的最後一個代碼int。即使我複製無效,它不會清除我的形狀。會有什麼問題呢? – deadfish 2012-04-18 21:12:16

+0

它也發生在我身上,但無效()不是問題。其他代碼可能是問題,例如無效循環或繪製的東西沒有具體指出。 – 2012-04-23 08:59:10

+0

你是對的:) – deadfish 2012-04-23 10:38:12

1

請嘗試postInvalidate()不invalidate()。

該方法invalidate()不鼓勵在非UI線程中使用。