2015-10-18 73 views
0

我是Android开发的白痴。我开始制作我的第一个抵押贷款应用程序。我很难找到如何从与我的seekbar相关联的textview中获取价值,并将其除以12这是一年中的月数。搜索栏保存他们触摸/拖动搜索条所产生的用户利率。如果任何人都可以帮助它会很棒!谢谢!随意带我到任何可能有所帮助的教程。如何从TextView中获取值并将其除以年数?

package com.example.mrstreet.myapplication; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.EditText; 
import android.widget.SeekBar; 
import android.text.TextWatcher; 
import android.widget.TextView; 
import android.widget.SeekBar.OnSeekBarChangeListener; 

public class morcal extends Activity { 

    public double currentLoanAmount; // loan amount entered by user 
    public EditText loan_amount; //accepts user input for loan amount 

    public SeekBar InterestRate; //SeekBar 
    public TextView currentInterestRate; //Interest rate reflected from SeekBar 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_morcal); 


     loan_amount = (EditText) findViewById(R.id.loan_amount); //Get Loan EditText 

     loan_amount.addTextChangedListener(loan_amountWatcher); //Loan Amount Text Watcher 

     InterestRate = (SeekBar) findViewById(R.id.InterestRate); 
     currentInterestRate = (TextView) findViewById(R.id.currentInteresetRate); 

     InterestRate.setOnSeekBarChangeListener(new OnSeekBarChangeListener()); 
     { 
      //Listener for SeekBar 
      @Override 
      public void onProgressChanged(SeekBar InterestRate,int progress, boolean fromUser){ 
      float decimalProgress = (float) progress/10.0f; 
      System.out.println("---- " + decimalProgress); 
      currentInterestRate.setText(decimalProgress + "%"); //Seek Reflects to decimal progress 
     } 

      @Override 
      public void onStartTrackingTouch (SeekBar InterestRate){ 
     } 

      @Override 
      public void onStopTrackingTouch (SeekBar InterestRate){ 
     }); 

    public TextWatcher loan_amountWatcher = new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     //called when user enters the number 
     try 
     { 
      loan_amount = Double.parseDouble(s.toString()); 
     } 
     catch (NumberFormatException e){ 
     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }; 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_morcal, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 



Here is my xml layout. 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".morcal"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true"> 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Loan Amount"/> 

     <EditText 
      android:id="@+id/loan_amount" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
    <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:paddingTop="60dp" 
     android:layout_marginTop="40dp" 
     android:layout_marginBottom="110dp" 
     android:layout_alignParentEnd="false" 
     android:layout_alignParentStart="false" 
     android:layout_alignParentTop="false"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Interest Rate"/> 
     <SeekBar 
      android:id="@+id/InterestRate" 
      android:layout_width="250dp" 
      android:layout_height="wrap_content" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="%0.0" 
      android:id="@+id/currentInteresetRate" 
      android:layout_gravity="bottom|right|top" 
      android:layout_marginRight="50dp" 
      android:layout_marginBottom="200dp" 
      android:gravity="top" 
      android:textStyle="bold" /> 

    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="180dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Mortgage Period (Years)" 
      android:id="@+id/textView" /> 

     <RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal|left"> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="10yr" 
       android:id="@+id/tenYearButton1" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="15yr" 
       android:id="@+id/fifthteenYearButton2" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="30yr" 
       android:id="@+id/thirtyYearButton3" /> 
     </RadioGroup> 

    </LinearLayout> 

</RelativeLayout> 

回答

0

TextView的帮助方法getText与setText类似。

+1

'的EditText#getText'返回一个'Editable'不是'String'。 – JBirdVegas

0

如果你只是想从你的TextView得到一个号码,那么这应该为你做。

int loadAmount = Integer.parseInt(loan_amount.getText().toString()); 

请记住,你也应该限制哪些用户可以输入,因为如果进入了一个非数字值以上将抛出NumberParseException

+0

fyi:xml中TextView的inputType =“number” – Matt

+0

@Override public void onProgressChanged(SeekBar InterestRate,int progress,boolean fromUser){float} decimalProgress =(float)progress/10.0f; System.out.println(“----”+ decimalProgress); currentInterestRate.setText(decimalProgress +“%”); // Seek反射到十进制 int currentInterestRate = Integer.parseInt(currentInterestRate.getText()。toString()); – YungMula

0

更改此:

 //Listener for SeekBar 
     @Override 
     public void onProgressChanged(SeekBar InterestRate,int progress, boolean fromUser){ 
     float decimalProgress = (float) progress/10.0f; 
     currentInterestRate.setText(String.valueOf(decimalProgress) + "%"); 


说明:

当您使用的setText(),以一个TextView,你需要传递给它一个字符串,以前你直接传递一个浮点型变量:

currentInterestRate.setText(decimalProgress + "%"); 

使用String.valueOf(),您将您的decimalProcess浮点变量转换为Strin可以传递到的setText克()方法:

currentInterestRate.setText(String.valueOf(decimalProgress) + "%"); 
+0

在答案中增加更多解释。 – Alex

相关问题