2017-01-10 15 views
0

我想實現一個彈出窗口中的微調框。當選擇一個項目並單擊該按鈕時,它將根據微調器中的選定項目進行顯示。使用微調框作爲驗證到下一個活動

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); 
} 

不介意註釋代碼下面的代碼

+0

是什麼確切的問題和解決問題? – W4R10CK

+0

你在問什麼?問題是什麼? –

+0

我的問題是,當我點擊按鈕,我想它首先檢查選擇的項目例如現金,然後它應顯示一個敬酒說明現金選定 –

回答

0

使用彈出式窗口中添加微調,並把它選擇的項目敬酒彈出。

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(); 
    } 
}); 
+0

@Barbe Swach甘比諾試試這一個,讓我知道是否有任何問題 –

+0

我試過,選擇是好的,但我的按鈕不工作,這應該帶我到下一個活動 –

+0

把意圖我=新意圖(currentactivity.this,nextactivity.class)startintent(i);點擊按鈕,點擊 –

0

試試這個代碼:

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

謝謝大家。 Nidhi Pandya你爲我工作 –

+0

在我的佈局Nidhi Pandya我有一個按鈕,當我點擊後選擇一個項目應該帶我到下一個活動。請幫忙 –

相關問題