2016-02-12 54 views
1

我想知道如何启用当用户满足某些标准(即,高度和用于拼图板的尺寸宽度大于1)后,设置为false的按钮...机器人:启用/禁用按钮

因为,根据我下面的代码,该按钮保持在虚拟仿真器禁用:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

// Class used to retrieve user-input values in order to create the puzzle board based on its 
// dimensions as shown in the SecondActivity class. 
public class FirstActivity extends AppCompatActivity { 
// Initializes fields. 
    private EditText etHeight, etWidth; 
    private int height, width; 
    private Button instructions, play; 

    // Main method. 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Instantiates the following variables by reference ID. 
     instructions = (Button)findViewById(R.id.instructionsButton); 
     play = (Button)findViewById(R.id.playButton); 
     play.setEnabled(false); 

     instructions.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(FirstActivity.this, PopUpActivity.class); 
       startActivity(i); 
      } 
     }); 

     play.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Instantiates the components of a dimension with Android's interfaces. 
       etHeight = (EditText)findViewById(R.id.enterHeight); 
       String ht = etHeight.getText().toString(); 

       // Convert to other data types accordingly. 
       height = Integer.parseInt(ht); 

       etWidth = (EditText)findViewById(R.id.enterWidth); 
       String wh = etWidth.getText().toString(); 
       width = Integer.parseInt(wh); 

       if (height > 1 && width > 1) { 
        play.setEnabled(true); 
       } 

       Intent i = new Intent(FirstActivity.this, SecondActivity.class); 
       // Compile data to the Bundle interface. 
       i.putExtra("height", height); 
       i.putExtra("width", width); 
       //v.setEnabled(true); 
       startActivity(i); 
      } 
     }); 
    } 

谢谢!

+2

你已经在这里禁用了你的按钮:'play.setEnabled(false);'你的问题是什么? –

+0

你已经做到了,只需要验证用户输入的拼图大小...一个微调或文本视图与正则表达式? –

+0

@ dpark14你想在用户填充宽度和高度edittexts后启用播放按钮吗? –

回答

1

只要文本满足特定条件,就可以在编辑文本的onTextChanged方法中将set enabled设置为true。

+0

非常感谢!我发现TextWatcher界面绝对适用于我的程序。 – DaveNOTDavid