0

我有一个文本视图,通过单击 TextView 我有一个带有 3 个 NumberPicker 的对话框。我想将对话框中显示的所有 3 个不同 NumberPickers 中的值连接到字符串,该字符串看起来像 '99 9 99' 或 '99999'[从 NumberPickers 中选择的整数],并将字符串设置为 textView。

public void pricePTola() {

    //int price;
    //TextView;
    //double RPT;
    NumberPicker np1, np2, np3;
    final int npThousand, npHundrd, npTen;
    //final String[] price = new String[1];
    String price;

    TextView textView = (TextView)findViewById(R.id.RPT);
    Dialog dialog = new Dialog(HomeActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
    dialog.setContentView(R.layout.number_picker);
    dialog.setTitle("RATE PER TOLA");
    //dialog.set

    //String strPrice = String.valueOf(price);
    //TextView RPT=(TextView)findViewById(R.id.RPT);

    np1=(NumberPicker)dialog.findViewById(R.id.np1);
    np1.setMaxValue(99);
    np1.setMinValue(00);
    np1.setWrapSelectorWheel(true);
    npThousand=np1.getValue();
    //npThousand=np1.toString();

    np2=(NumberPicker)dialog.findViewById(R.id.np2);
    np2.setMaxValue(9);
    np2.setMinValue(0);
    np1.setWrapSelectorWheel(true);
    npHundrd=np2.getValue();
    //npHundrd=np2.toString();

    np3=(NumberPicker)dialog.findViewById(R.id.np3);
    np3.setMaxValue(99);
    np3.setMinValue(00);
    np1.setWrapSelectorWheel(true);
    npTen=np3.getValue();
    np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            picker.setValue((newVal < oldVal)?oldVal-5:oldVal+5);
            /*price[0] = npThousand + "" + npHundrd + "" + npTen;
            TextView RPT = (TextView) findViewById(R.id.RPT);
            RPT.setText(price[0]);*/
        }
    });
    /*np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            picker.setValue((newVal < oldVal)?oldVal-5:oldVal+5);
            price[0] = npThousand + "" + npHundrd + "" + npTen;
            TextView RPT = (TextView) findViewById(R.id.RPT);
            RPT.setText(price[0]);
        }
    });
    np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            picker.setValue((newVal < oldVal)?oldVal-5:oldVal+5);
            price[0] = npThousand + "" + npHundrd + "" + npTen;
            TextView RPT = (TextView) findViewById(R.id.RPT);
            RPT.setText(price[0]);
        }
    });
    dialog.show();

    //price = valueOf(valueOf(np1) + valueOf(np2) + valueOf(np3));
    price = npThousand+""+ npHundrd+""+npTen;
    TextView RPT=(TextView)findViewById(R.id.RPT);
    RPT.setText(price);

}

MainActivity.Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    final TextView TV1=(TextView)findViewById(RPT);
    TV1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pricePTola();

        }
    });
4

1 回答 1

1

1-我看到你应该让函数 pricePTola 返回字符串,你不应该在上面声明 TextView。

2 - 在每个 onvalueChange 函数的函数 pricePTola 中,您必须输入 npThousand=np1.getValue(); 对于 np1 并为其他两个选择器执行相同的操作。

3-函数结束 String result = String.valueOf(npThousand)+String.valueOf(npHundrd)+String.valueOf(npTen); return result;

4 - 终于在 oncreate

TV1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String final = pricePTola();
        TV1.setText(final);

    }
});

我希望这有帮助

于 2017-08-16T15:00:57.567 回答