gpt4 book ai didi

android - TextChanged 监听器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:53 25 4
gpt4 key购买 nike

我仍然是 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;
}
}

User interface

最佳答案

在 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();

}
});

关于android - TextChanged 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com