我是Android开发的佼佼者。我开始建立我的第一个抵押贷款应用程序。我很难找到如何从与我的搜索栏相关的文本视图中获取值并将其除以12,这是一年中的月数。搜索栏保持用户通过触摸/拖动搜索条生成的利率。如果有人可以帮助它会很棒!谢谢!随意带我去任何可能有用的教程。
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 :(得分:0)
TextView具有与setText类似的辅助方法getText。
答案 1 :(得分:0)
如果您只是希望从TextView
获取一个号码,那么这应该为您完成。
int loadAmount = Integer.parseInt(loan_amount.getText().toString());
请注意,您还应该限制用户可以输入的内容,因为如果输入了非数字值,上面将抛出NumberParseException
。
答案 2 :(得分: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时,你需要传递一个String,之前你直接传递一个float变量:
currentInterestRate.setText(decimalProgress + "%");
使用String.valueOf(),您可以将decimalProcess float变量转换为可以传递给setText()方法的String:
currentInterestRate.setText(String.valueOf(decimalProgress) + "%");