android-TextChanged侦听器

android-TextChanged侦听器,第1张

概述我还是Android开发的新手,我想做的是创建一个侦听器,该侦听器将包含两个保存区域和周长的TextView对象.宽度和高度是EditText对象.输入宽度和高度后,应基于calcArea和CalcPerimeter方法实时显示周长和面积的值.我用于侦听器的代码基于我在此处找到的示例.我的代码:packagecom

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

解决方法:

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

以上是内存溢出为你收集整理的android-TextChanged侦听器全部内容,希望文章能够帮你解决android-TextChanged侦听器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088640.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存