连击数串由不同数量的采摘获取和显示一个TextView

问题描述:

I have a text view, by clicking TextView I have a dialog with 3 NumberPickers. I want to concatenate values to string which will looks like '99 9 99' or '99999'[selected integers from NumberPickers] from all 3 different NumberPickers shown in dialog, and set the string to the textView.连击数串由不同数量的采摘获取和显示一个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(); 

     } 
    }); 

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

2 - 在函数pricePTola上的每个onvalueChange函数中,您必须将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); 

    } 
}); 

我希望这有助于

+0

我做 我做np1.setOnValueChangedListener(新NumberPicker.OnValueChangeListener(){ @Override public void onValueChange(NumberPicker numberPicker,int i,int i1){ final int npHundrd = np2.getValue(); } }); –

+0

我正在做2种方法。第一是这样的: np1.setOnValueChangedListener(新NumberPicker.OnValueChangeListener(){ @Override 公共无效onValueChange(NumberPicker numberPicker,INT I,INT I1){ 最终诠释npHundrd = np2.getValue();} } ) ;但在这里当我做'字符串结果= String.valueOf(valueOf(npThousand)+ valueOf(npHundrd)+ valueOf(npTen)); 'npThousand/Hundrd/Ten在这里没有工作。那么如果我尝试'int npTen = np3.getValue(); '在'OnValueChangedListene'之外放置它会在TextView中显示一些垃圾值。 –

+0

它继续在文本视图中扔垃圾值。它显示文本中的值当对话框打开时显示视图,意味着文本视图在对话框打开时获取值时不会在对话框关闭或numberPicker值更改时获取/获取值。 –