0

我这里有问题。我想通过使用复选框来禁用编辑文本。如果复选框被选中,edittext 是可用的,如果没有,它被禁用。但我遇到了问题。这是我的代码:任何人都可以检查一下。Accdng 到 Eclipse,没有错误。但是当我在手机上运行它时,启用/禁用不起作用。

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

}
4

1 回答 1

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);
}
于 2013-08-27T23:32:29.867 回答