2014-01-23 49 views
4
// All required Imports 

public class PuzzleActivity extends Activity implements OnClickListener{ 
    private PuzzlerDB db; 
    private Puzzle puz; 

    private int puzId=1;  
    private String puzAns=""; 
    private int score=0; 

    private TextView tvPuzContent; 
    private EditText etPuzAns; 
    private Button btnCheck; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

     setContentView(R.layout.activity_puzzle); 

     if (customTitleSupported) { 
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 
     } 

     db = new PuzzlerDB(this); 
     puz = db.getPuzzle(puzId); 
     puzAns = puz.getAnswer(); 

     tvPuzContent = (TextView) findViewById(R.id.tv_puz_content); 
     tvPuzContent.setText(puz.getContent()); 
     btnCheck = (Button) findViewById(R.id.btn_check); 
     btnCheck.setOnClickListener(this); 

     android.util.Log.v("IN SIDE ACTIVITY: ", "id: "+ puzId + " answer: "+puz.getAnswer()); 

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.btn_check: 
      checkAndNotify(puzAns); 
      break; 
     }  
    } 

    private void checkAndNotify(String puzAns) 
    { 
     String ans = ((EditText)findViewById(R.id.te_puz_ans)).getText().toString(); 

     if(ans.trim().equalsIgnoreCase(puzAns.trim())) 
     { 
      //show pop-up dialog for right answer 
      new AlertDialog.Builder(this).setTitle("The Puzzler:") 
       .setMessage("Wow! Right Answer!!") 
       .setNeutralButton("Continue", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dlg, int sumthin) 
        { 
         android.util.Log.v("IN SIDE function:", " onClick of Dialog"); 
         update(); 
        } 
       }).show(); 

     }else{ 
      new AlertDialog.Builder(this).setTitle("The Puzzler:") 
      .setMessage("Sorry! You have to rethink!!") 
      .setNeutralButton("Continue", new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dlg, int sumthin) 
       { 
        // do nothing – it will close on its own 
       } 
      }).show();   
     } 
    } 

    protected void update() { 
     score = score + 10; 
     ++puzId;      
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setContentView(R.layout.activity_puzzle); 
     puz = db.getPuzzle(puzId); 
     if(puz != null) 
     { 
      tvPuzContent.setText(puz.getContent()); 
      android.util.Log.v("IN SIDE onResume:", " Content: "+ puz.getContent()); 
      puzAns = puz.getAnswer();   
     } 
    } 

    @Override 
    protected void onDestroy() { 
      super.onDestroy(); 
      db.close(); 
    } 
} 

活動佈局如下:爲什麼setText()在TextView的以下代碼中不起作用?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bgimg" 
    android:layout_gravity="center"> 

    <TableLayout android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_margin="20dp" 
     android:stretchColumns="*"> 

     <TableRow>  
      <TextView android:id="@+id/tv_puz_content" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_span="3" 
       android:typeface="serif" 
       android:textStyle="bold" 
       android:textSize="20sp" 
       android:text="" /> 
     </TableRow> 

     <TableRow>   
      <EditText android:id="@+id/te_puz_ans" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_span="3"    
       android:hint="Answer" /> 
     </TableRow> 

     <TableRow android:gravity="center">   
      <Button android:id="@+id/btn_check" 
       android:layout_width="60dp" 
       android:layout_height="wrap_content" 
       android:hint="Check" /> 

      <Button android:id="@+id/btn_skip" 
       android:layout_width="60dp" 
       android:layout_height="wrap_content" 
       android:hint="Skip" /> 

      <Button android:id="@+id/btn_hint" 
       android:layout_width="60dp" 
       android:layout_height="wrap_content" 
       android:hint="Hint" /> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

的數據庫查詢返回正確的內容,我可以通過logcat的檢查。但文本視圖沒有顯示出來。EditText和按鈕正在顯示。 (i)使TextView靜態(ii)接收字符串中的拼圖內容,然後通過setText()和其他方式將其設置爲不成功。你能否提出一個解決方案?

+0

你在puz var中得到了什麼嗎? – Saqib

+0

你在這個日誌中得到什麼? android.util.Log.v(「IN SIDE onResume:」,「Content:」+ puz.getContent()); – pozuelog

+0

是的。 Puz擁有其所有數據成員的價值。我在上面的日誌中獲得了這個難題的內容,例如:'01 -23 22:28:17.937:V/IN SIDE onResume:(18673):內容:2 x 5的值是多少?' – Dexter

回答

8

您調用onResume()setContentView(R.layout.activity_puzzle);,但tvPuzContent變量在onCreate()實例化,所以它是指從onCreate原來的呼叫setContentView文本視圖。

基本上由onCreate年底分配了所有的變量,但隨後onResume由系統調用,你再打電話setContentView,其扔掉原來的佈局,讓所有的變量都在那個老觀點指點不在屏幕上。

只需在onResume()中不要撥setContentView,因爲您無論如何都不會更改佈局。

+0

感謝您的回答。我糾正了那部分。我從onResume()中移除了setContentView()並獲取了屏幕上的內容。但是爲什麼onResume()在關閉警報對話框後沒有被調用?如果我沒有錯,就應該調用onResume()之後活動已經到達前臺。這應該通過getPuzzle()獲得新的難題,並且應該更新textview?我如何重新繪製新的拼圖內容? – Dexter

+1

我不認爲一個對話框會把你的活動放到後臺 - 對話本質上是活動中的另一個視圖。只有其他活動可以使您的活動進入後臺。如果新的活動是透明的或者不是全屏的,它只會觸發onPause/onResume而不會觸發onStop/onRestart/onStart。如果您希望在對話框關閉時發生某些情況,請爲其創建方法,然後從對話框的按鈕偵聽器中調用該方法。 – Tenfour04

+0

這就是要點。我的印象是,當任何事情阻止一項活動時,它就是「onPaused」。但是,當阻塞元素(Dialog是這種情況)是Activity本身的另一個組件時就不是這種情況。爲此+1。謝謝。 – Dexter

相關問題