简体   繁体   English

如何在同一活动中将2个EditText保存到2个微调框

[英]How to use save 2 editText to 2 spinners in same Activity

I want to make the second "save" button save the second editText to second spinner like the first does this is the code I'm trying to use and thanks to cricket_007 for helping me with the whole question 我想使第二个“保存”按钮将第二个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) {
        }

布局捕捉

You only need to setup the adapter and spinner's once. 您只需要设置适配器和微调器一次。

Whatever you are trying to do between the click events, just gets added into onItemSelected , but in order to set the selection of the other spinner, it needs some data, so add an adapter to it. 无论您在单击事件之间尝试做什么, onItemSelected添加到onItemSelected ,但是为了设置其他微调器的选择,它需要一些数据,因此请向其添加适配器。

And, for your latest edit of the question, you've not indented the code correctly, but you've set some stuff within the onClick of the button that shouldn't be there. 而且,对于问题的最新编辑,您没有正确缩进代码,但已在按钮的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);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM