Android 文本更改侦听器
我还是Android开发的新手,我想做的是创建一个监听器,其中包含两个包含面积和周长的TextView对象。宽度和高度是EditText对象。输入宽度和高度后,周长和面积的值应根据Calcrea和calcPerimeter方法实时显示。我用于侦听器的代码基于我在这里找到的一个示例。我的代码:Android 文本更改侦听器,android,Android,我还是Android开发的新手,我想做的是创建一个监听器,其中包含两个包含面积和周长的TextView对象。宽度和高度是EditText对象。输入宽度和高度后,周长和面积的值应根据Calcrea和calcPerimeter方法实时显示。我用于侦听器的代码基于我在这里找到的一个示例。我的代码: package com.jtryon.rectanglecalc; import android.support.v7.app.ActionBarActivity; import
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;
}
}
您能否稍微澄清一下,在一个侦听器中使用两个文本视图可以实现什么目的。我的第一个建议是创建一个接受textview作为参数的回调。
然后让您的类接受由这两个类创建的两个回调。不确定问题。在两个
EditText
视图中添加一个OnFocusChangeListener
当它没有聚焦时,获取值并进行计算并更新TextView
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
// add your code to process the values entered
}
}
});
请尝试以下代码-
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>
希望这对你有帮助
如果它不起作用,请告诉我,我会尽力帮助你的 在onCreate中的编辑文本下定义此项
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();
}
});
我试图发布一个截图,但我没有足够的声誉点。问题是它必须实时更新,我猜我需要使用某种侦听器。一旦焦点离开EditText对象,它就必须更新,因此无法确定如何实现代码。我在原始帖子中添加了一个图像,以显示我希望完成的任务。这不是答案,这应该是一条注释。如果没有50 rep,则无法进行注释OnFocusChangeListener和AddTextChangedListener有什么区别?您创建区域和周长EditText对象而不是TextView对象的原因是什么?我只想编辑宽度和高度,然后在此基础上,周长和面积应该更新。现在你可以检查我编辑的答案。我将editText更改为textview。
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();
}
});