我想制作第二个“保存”按钮将第二个editText保存到第二个微调器,就像第一个,这是我正在尝试使用的代码,感谢cricket_007帮助我解决整个问题
List<String> pn, txt;
Spinner sp1, sp2;
Button b, b1, b2, b3;
EditText et, et1;
ArrayAdapter<String> adp1, adp2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setupSpinners();
b = (Button) findViewById(R.id.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
et = (EditText) findViewById(R.id.editText);
et1 = (EditText) findViewById(R.id.editText1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp1.add(et.getText().toString());
et.setText(null);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp2.add(et1.getText().toString());
et1.setText(null);
}
});
}
public void setupSpinners() {
sp1 = (Spinner) findViewById(R.id.spinner);
sp2 = (Spinner) findViewById(R.id.spinner1);
pn = new ArrayList<String>();
txt = new ArrayList<String>();
txt.add("Sorry Boss I'cant go today i'm sick");
pn.add("123456789");
adp1 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, pn);
adp2 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, txt);
sp1.setAdapter(adp1);
sp1.setSelection((pn.size() - 1));
sp2.setAdapter(adp2);
sp2.setSelection((txt.size() - 1));
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp1.setSelection(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp2.setSelection(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
答案 0 :(得分:0)
您只需要设置适配器和微调器一次。
无论您在点击事件之间尝试做什么,只需添加到onItemSelected
,但为了设置其他微调器的选择,它需要一些数据,因此添加适配器。
并且,对于您对问题的最新修改,您还没有正确缩进代码,但是您已经在按钮的onClick
内设置了一些不应该是那里。
public class MainActivity extends AppCompatActivity {
List<String> pn, txt;
Spinner sp1, sp2;
Button b, b1, b2, b3;
EditText et, et1;
ArrayAdapter<String> adp1, adp2; // Added
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupSpinners();
b = (Button) findViewById(R.id.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
et = (EditText) findViewById(R.id.editText);
et1 = (EditText) findViewById(R.id.editText1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp1.add(et.getText().toString()); // Changed
et.setText("");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp2.add(et1.getText().toString());
et1.setText("");
}
});
}
public void setupSpinners() {
sp1 = (Spinner) findViewById(R.id.spinner);
sp2 = (Spinner) findViewById(R.id.spinner1);
pn = new ArrayList<String>();
txt = new ArrayList<String>();
txt.add("Sorry Boss I'cant go today i'm sick");
pn.add("123456789");
adp1 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, pn);
adp2 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, txt);
sp1.setAdapter(adp1);
sp1.setSelection((pn.size() - 1));
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp2.setSelection(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp2.setAdapter(adp2);
}
}