简体   繁体   中英

How to display number pickers in pop up window

how to display the number pickers in the pop up window...

I tried making the number pickers in different activity..but i want it to be displayed in a pop up window on a button click.

    package com.example.uc232142.picker;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.NumberPicker;
    import android.widget.PopupWindow;
    import static android.app.PendingIntent.getActivity;
    public class MainActivity extends AppCompatActivity {
    NumberPicker numberpicker,numberPicker1,numberPicker2;
    final String month[] = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
    final String sub[] ={"Airlaw","criminal","Income tax","Direct tax","Customs", "Defence & Security Forces" , "Disinvestment" , "Education" , "Election" ,
                         "Electricity & Energy" , "Environment, Wildlife & Animal", "Exchange Control & FDI " , "Excise"};
    // TextView textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picker);
        // final int popup=0x7f04002b;
        numberpicker = (NumberPicker) findViewById(R.id.numberPicker1);
        numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker2);
        numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker3);
        final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        final View popupView = layoutInflater.inflate(R.layout.picker, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popupWindow.showAsDropDown(button, 10, -10);
                show();
            }
        });
    }
    public void show(){
        numberpicker = (NumberPicker) findViewById(R.id.numberPicker1);
        numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker2);
        numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker3);
        numberpicker.setMinValue(1950);
        numberpicker.setMaxValue(2018);
        numberpicker.setValue(2017);
        numberPicker1.setMinValue(0);
        numberPicker1.setMaxValue(month.length - 1);
        numberPicker1.setDisplayedValues(month);
        numberPicker1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        numberPicker2.setMinValue(0);
        numberPicker2.setMaxValue(sub.length - 1);
        numberPicker2.setDisplayedValues(sub);
        numberPicker2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    }
}

I've tried using AlertDialog instead of PopupWindow. It's working fine on my side, give it a try if it's what you are looking for.

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity {
    final String month[] = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
    final String sub[] ={"Airlaw","criminal","Income tax","Direct tax","Customs", "Defence & Security Forces" , "Disinvestment" , "Education" , "Election" ,
            "Electricity & Energy" , "Environment, Wildlife & Animal", "Exchange Control & FDI " , "Excise"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageButton button = (ImageButton) findViewById(R.id.imageButton);            
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openDialog();
            }
        });
    }

    /**
     * This will construct An {@link AlertDialog} with some custom views.
     */
    private void openDialog() {
        //Inflating a LinearLayout dynamically to add TextInputLayout
        //This will be added in AlertDialog
        final LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.view_number_dialog, null);
        NumberPicker numberpicker = (NumberPicker) linearLayout.findViewById(R.id.numberPicker1);
        NumberPicker numberPicker1 = (NumberPicker) linearLayout.findViewById(R.id.numberPicker2);
        NumberPicker numberPicker2 = (NumberPicker) linearLayout.findViewById(R.id.numberPicker3);

        numberpicker.setMinValue(1950);
        numberpicker.setMaxValue(2018);
        numberpicker.setValue(2017);
        numberPicker1.setMinValue(0);
        numberPicker1.setMaxValue(month.length - 1);
        numberPicker1.setDisplayedValues(month);
        numberPicker1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        numberPicker2.setMinValue(0);
        numberPicker2.setMaxValue(sub.length - 1);
        numberPicker2.setDisplayedValues(sub);
        numberPicker2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        //Finally building an AlertDialog
        final AlertDialog builder = new AlertDialog.Builder(this)
                .setPositiveButton("Submit", null)
                .setNegativeButton("Cancel", null)
                .setView(linearLayout)
                .setCancelable(false)
                .create();
        builder.show();
        //Setting up OnClickListener on positive button of AlertDialog
        builder.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Code on submit
            }
        });
    }
}

view_number_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="260dp"
    android:orientation="horizontal"
    android:padding="14dp">

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <NumberPicker
        android:id="@+id/numberPicker2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="14dp"
        android:layout_marginRight="14dp"
        android:layout_weight="1" />

    <NumberPicker
        android:id="@+id/numberPicker3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

It'll output as following...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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