我需要在一个活动中放置三个数字选择器。
我的问题是,当我单击屏幕时,所有这些按钮都“变为活动状态”。
我希望每个人在单击相应按钮时都“活跃”起来。 我认为问题出在“ onClick”功能上,但我不知道如何解决。
public class personal_data extends AppCompatActivity implements View.OnClickListener {
TextView e_number;
Button e_numberpicker;
TextView a_number;
Button a_numberpicker;
TextView p_number;
Button p_numberpicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_personal_data);
e_number = (TextView) findViewById(R.id.t_e);
e_numberpicker = (Button)findViewById(R.id.b_e);
e_numberpicker.setOnClickListener(this);
a_number = (TextView) findViewById(R.id.t_a);
a_numberpicker = (Button)findViewById(R.id.b_a);
a_numberpicker.setOnClickListener(this);
p_number = (TextView) findViewById(R.id.t_p);
p_numberpicker = (Button)findViewById(R.id.b_p);
p_numberpicker.setOnClickListener(this);
}
@Override
public void onClick(View v){
e_setNumberpicker();
a_setNumberpicker();
p_setNumberpicker();
}
private void e_setNumberpicker() {
NumberPicker e_myNumberPicker = new NumberPicker(this);
e_myNumberPicker.setMaxValue(110);
e_myNumberPicker.setMinValue(0);
NumberPicker.OnValueChangeListener myValChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
e_number.setText(""+newVal);
}
};
e_myNumberPicker.setOnValueChangedListener(myValChangedListener);
AlertDialog.Builder e_builder = new AlertDialog.Builder(this).setView(e_myNumberPicker);
e_builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
}
});
e_builder.show();
}
private void a_setNumberpicker() {
NumberPicker a_myNumberPicker = new NumberPicker(this);
a_myNumberPicker.setMaxValue(210);
a_myNumberPicker.setMinValue(0);
NumberPicker.OnValueChangeListener myValChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
a_number.setText(""+newVal);
}
};
a_myNumberPicker.setOnValueChangedListener(myValChangedListener);
AlertDialog.Builder a_builder = new AlertDialog.Builder(this).setView(a_myNumberPicker);
a_builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
}
});
a_builder.show();
}
private void p_setNumberpicker() {
NumberPicker p_myNumberPicker = new NumberPicker(this);
p_myNumberPicker.setMaxValue(150);
p_myNumberPicker.setMinValue(0);
NumberPicker.OnValueChangeListener myValChangedListener = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
p_number.setText(""+newVal);
}
};
p_myNumberPicker.setOnValueChangedListener(myValChangedListener);
AlertDialog.Builder p_builder = new AlertDialog.Builder(this).setView(p_myNumberPicker);
p_builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
}
});
p_builder.show();
}
}
有什么建议吗?
非常感谢您
答案 0 :(得分:0)
您需要检查哪个视图触发了onClick
@Override
public void onClick(View v){
if (v == e_numberpicker) {
e_setNumberpicker();
}
if (v == a_numberpicker) {
a_setNumberpicker();
}
if (v == [_numberpicker) {
p_setNumberpicker();
}
}
答案 1 :(得分:0)
您还可以将 onClick监听器附加到单个TextView
TextView e_number = (TextView) findViewById(R.id.t_e);
e_number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});