如何在弹出窗口中显示数字选择器

时间:2017-08-22 08:29:38

标签: android popup android-6.0-marshmallow

如何在弹出窗口中显示数字选择器...

我尝试在不同的活动中制作号码选择器..但我希望它在按钮点击的弹出窗口中显示。

var mainARR = [[1, 2, 3, 4], [5, 6, 7, 8]],
    delARR = [1, 2, 3, 4],
    result = mainARR.map(a => a.filter(b => !delARR.includes(b))).filter(a => a.length);

console.log(result);

1 个答案:

答案 0 :(得分:1)

我尝试过使用AlertDialog而不是PopupWindow。它在我身边工作得很好,试试看你想要的东西。

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>

它输出如下......

enter image description here