gpt4 book ai didi

java - 使用复选框错误禁用编辑文本

转载 作者:行者123 更新时间:2023-12-01 14:11:49 24 4
gpt4 key购买 nike

我这里有个问题。我想通过使用复选框禁用编辑文本。如果选中复选框,则编辑文本可用,如果没有,则禁用。但我遇到了问题。这是我的代码:任何人都可以检查一下吗?按照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());
}
}

}

最佳答案

您无法通过调用方法 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);
}

关于java - 使用复选框错误禁用编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com