我是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>
'的EditText#getText'返回一個'Editable'不是'String'。 – JBirdVegas