簡體   English   中英

將微調框用作下一個活動的驗證

[英]Use spinner as Validation to next Activity

我正在嘗試實現彈出窗口中的微調器。 選擇一個項目並單擊按鈕時,它將根據微調器中的選定項目顯示。

String[]Company={"Cash","M-Pesa","Voucher","Credit-Card"};

以下是包含微調器的彈出窗口

 private void callPopup() {

    LayoutInflater layoutInflater=(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView=layoutInflater.inflate(R.layout.popup1,null);

    //final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
    final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);

    popupWindow.setTouchable(true);
    popupWindow.setFocusable(true);

    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

    final Spinner popupSpinner=(Spinner)popupView.findViewById(R.id.spinner);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(StartWatchActivity.this,android.R.layout.simple_spinner_dropdown_item, Company);
    popupSpinner.setAdapter(adapter);


    Name =(EditText)popupView.findViewById(R.id.edtimageName);
    Name.setText(String.valueOf(amount));
    final Spinner spnLocale;
    spnLocale=(Spinner)findViewById(R.id.spinner);
    //int iCurrentSelection=spnLocale.getSelectedItemPosition();
   // TextView txtView = (TextView)popupView.findViewById(R.id.txtView);
   // txtView.setText("Total Cars Packed:\t" +amount +"  Cars");
            ((Button) popupView.findViewById(R.id.saveBtn)).setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        public void onClick(View v) {

            spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (!(spnLocale.getSelectedItem().toString().trim().equals("Company"))) {

                        if (spnLocale.getSelectedItem().toString().trim().equals("Cash")) {

                            Toast.makeText(StartWatchActivity.this, "Amount Paid :\t" + Name.getText().toString(), Toast.LENGTH_LONG).show();
                        } else if (spnLocale.getSelectedItem().toString().trim().equals("M-pesa")) {
                            Toast.makeText(StartWatchActivity.this, "Amount Paid :\t" + Name.getText().toString(), Toast.LENGTH_LONG).show();
                        }
                    }

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
            //  Toast.makeText(getBaseContext(), "Amount paid", Toast.LENGTH_SHORT).show();
            // Toast.makeText(getApplicationContext(), Name.getText().toString(), Toast.LENGTH_LONG).show();


            popupWindow.dismiss();

        }
    });
    ((Button)popupView.findViewById(R.id.cancelbutton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            popupWindow.dismiss();

        }
    });

    popupWindow.showAsDropDown(saveBtn, 50,-30);
}

介意注釋的代碼

使用下面的代碼在彈出窗口中添加微調器,並將其選擇為吐司。

private void openSpinnerpopup() {

    //inflate the layout
    LayoutInflater li = LayoutInflater.from(MainActivity.this);

    View promptsView = li.inflate(R.layout.my_dialog_layout, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
    //set the inflated layout in the dialog.
    alertDialogBuilder.setView(promptsView);

    // create alert dialog
    alertDialog = alertDialogBuilder.create();

    final Spinner mSpinner = (Spinner) promptsView
            .findViewById(R.id.spinner);

    // reference UI elements from my_dialog_layout in similar fashion
    mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());

    // show it
    alertDialog.show();
    alertDialog.setCancelable(true);
}

//for spinneritemclick.
public class OnSpinnerItemClicked implements AdapterView.OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent,
                               View view, int pos, long id) {
        Toast.makeText(parent.getContext(), "Selected : " +
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        btn.setText(parent.getSelectedItem().toString());
    }

    @Override
    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

在buttonclick上調用它:

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        //open spinner  as dialog
        openSpinnerpopup();
    }
});

試試這個代碼:

 String name= null;
if(popupSpinner != null && popupSpinner.getSelectedItem() !=null ) {
   name = (String)popupSpinner.getSelectedItem();
   //get the name of current selected item..
} else  { 
   //nothing is selected
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM