2013-08-27 58 views
0

我在這裏遇到問題。我想通過使用複選框來禁用edittext。如果複選框被選中,edittext是可用的,否則它被禁用。但我有問題。這是我的代碼:任何人都可以檢查這個。加入日食,沒有錯誤。但是當我在手機上運行它時,啓用/禁用功能不起作用。使用複選框錯誤禁用edittext

public class Order extends Activity { 
private Button button1; 
private EditText txtbox1,txtbox2; 
private TextView tv; 
CheckBox check1; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.order); 
    txtbox1= (EditText) findViewById(R.id.editText1); 
    button1 = (Button) findViewById(R.id.button1); 
    tv = (TextView) findViewById(R.id.editText5); 
    txtbox2= (EditText) findViewById(R.id.editText2); 
    check1 = (CheckBox)findViewById(R.id.checkcheck); 
    button1.setOnClickListener(new clicker()); 


} 
public void addListenerOncheck1() { 

    check1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 
       if(isChecked){ 
        txtbox1.setFocusable(true); 
     txtbox1.setFocusableInTouchMode(true); 
       } else { 
        txtbox1.setFocusable(false); 
     txtbox1.setFocusableInTouchMode(false); 
       } 

      } 
     }); 
    } 

class clicker implements Button.OnClickListener 
{ 
    public void onClick(View v) 
    { 
     String a,b; 
     Integer vis; 
     a = txtbox1.getText().toString(); 
     b = txtbox2.getText().toString(); 
     vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3; 
     tv.setText(vis.toString()); 
    } 
} 

} 

回答

1

你無處通過調用烏爾法addListenerOncheck1()添加監聽器; 所以把addListenerOncheck1();添加你的OnCreate方法的結尾。

但我強烈建議您不要使用所有這些額外的自制類來添加偵聽器。使用下面的代碼,並將其添加到您的OnCreate方法:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.order); 
    txtbox1= (EditText) findViewById(R.id.editText1); 
    button1 = (Button) findViewById(R.id.button1); 
    tv = (TextView) findViewById(R.id.editText5); 
    txtbox2= (EditText) findViewById(R.id.editText2); 
    check1 = (CheckBox)findViewById(R.id.checkcheck); 
    button1.setOnClickListener(new clicker()); 
    check1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) { 
      if (isChecked) { 
       txtbox1.setEnabled(false); 
      } else { 
       txtbox1.setEnabled(true); 
      } 
     } 
    }); 

    button1.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      String a,b; 
      Integer vis; 
      a = txtbox1.getText().toString(); 
      b = txtbox2.getText().toString(); 
      vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3; 
      tv.setText(vis.toString()); 
     }   
    }); 

    // This will make sure the user can only type numbers into the EditTexts: 
    txtbox1.setInputType(InputType.TYPE_CLASS_NUMBER); 
    txtbox2.setInputType(InputType.TYPE_CLASS_NUMBER); 

    // Code to disable editTexts at start once: 
    txtbox1.setEnabled(false); 
    txtbox2.setEnabled(false); 
} 
+0

我還有一個問題。我希望我的edittext最初被禁用。只有當我檢查複選框時纔會啓用它。發生的是,當我啓動應用程序時,複選框沒有檢查,但我可以編輯edittext。要禁用edittext,我必須檢查然後取消選中文本框。你可以幫我嗎? – Kerv

+0

然後你還需要setEnabled(false); EditTexts onCreate;代碼被添加到答案中 – MojioMS