我仍然是Android开发的新手,我想要做的是创建一个包含两个TextView对象的侦听器,这些对象保存区域和周边。宽度和高度是EditText对象。在输入宽度和高度后,应根据calcArea和calcPerimeter方法实时显示周长和面积的值。我用于监听器的代码基于我在这里找到的一个例子。我的代码:
package com.jtryon.rectanglecalc;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
// fields in the class
// variables that are global to this file
double width;
double height;
double area;
double perimeter;
// "handles" to the objects from the XML
EditText widthEdit;
EditText heightEdit;
TextView areaText;
TextView perimText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set up handles
widthEdit = (EditText)findViewById(R.id.width_edit);
heightEdit = (EditText)findViewById(R.id.height_edit);
areaText = (TextView)findViewById(R.id.area_value);
perimText = (TextView)findViewById(R.id.perim_value);
widthEdit.addTextChangedListener(
new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
// read the width out of widthEdit
String widthString = widthEdit.getText().toString();
// convert the String into a double
if (widthString.length() > 0) {
width = Double.parseDouble(widthString);
}
// read the height out of heightEdit
String heightString = heightEdit.getText().toString();
if (heightString.length() > 0) {
height = Double.parseDouble(heightString);
}
// calculate area
double area = calcArea();
// calculate perimeter
double perim = calcPerim();
// set the label for areaText
areaText.setText(Double.toString(area));
// set the label for perimText
perimText.setText(Double.toString(perim));
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
}
);
}
double calcArea()
{
return width * height;
}
double calcPerim()
{
return 2 * width * height;
}
}
答案 0 :(得分:3)
在onCreate
中的edittext下定义 youredittext.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int aft )
{
}
@Override
public void afterTextChanged(Editable s)
{
//call your function here of calculation here
yourfunctioname();
}
});
答案 1 :(得分:1)
试用此代码 -
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="width" />
<EditText
android:id="@+id/edit_width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="height" />
<EditText
android:id="@+id/edit_height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="area" />
<TextView
android:id="@+id/edit_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="perimeter" />
<TextView
android:id="@+id/edit_perimeter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
MainActivity.java -
public class MainActivity extends ActionBarActivity {
EditText edit_width, edit_height;
TextView edit_area, edit_perimeter;
double width;
double height;
double area;
double perimeter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_area = (TextView) findViewById(R.id.edit_area);
edit_height = (EditText) findViewById(R.id.edit_height);
edit_width = (EditText) findViewById(R.id.edit_width);
edit_perimeter = (TextView) findViewById(R.id.edit_perimeter);
edit_width.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String widthString = edit_width.getText().toString();
// convert the String into a double
if (widthString.length() > 0) {
width = Double.parseDouble(widthString);
}
// read the height out of heightEdit
String heightString = edit_height.getText().toString();
if (heightString.length() > 0) {
height = Double.parseDouble(heightString);
}
// calculate area
double area = calcArea();
// calculate perimeter
double perim = calcPerim();
// set the label for areaText
edit_area.setText(Double.toString(area));
// set the label for perimText
edit_perimeter.setText(Double.toString(perim));
}
});
edit_height.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String widthString = edit_width.getText().toString();
// convert the String into a double
if (widthString.length() > 0) {
width = Double.parseDouble(widthString);
}
// read the height out of heightEdit
String heightString = edit_height.getText().toString();
if (heightString.length() > 0) {
height = Double.parseDouble(heightString);
}
// calculate area
double area = calcArea();
// calculate perimeter
double perim = calcPerim();
// set the label for areaText
edit_area.setText(Double.toString(area));
// set the label for perimText
edit_perimeter.setText(Double.toString(perim));
}
});
}
double calcArea() {
return width * height;
}
double calcPerim() {
return 2 * width * height;
}
}
希望这可以帮助你!!!!
如果它无法正常工作,请告诉我,我会尽力为您提供帮助..
答案 2 :(得分:0)
您是否可以通过听众中的两个文本视图来澄清您想要完成的内容。我的第一个建议是创建一个接受textview作为参数的回调。 然后让你的Class接受这两个类创建的2个回调。不确定这个问题。
答案 3 :(得分:0)
在OnFocusChangeListener
次观看中添加EditText
。
当它没有聚焦时,获取值并进行计算并更新TextView
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
// add your code to process the values entered
}
}
});