2014-01-23 50 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

相关问题