首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextChanged侦听器

TextChanged侦听器
EN

Stack Overflow用户
提问于 2014-04-24 00:57:45
回答 4查看 11K关注 0票数 4

我仍然是Android开发的新手,我想要做的是创建一个监听器,它将包含两个TextView对象,这些对象包含区域和周边。宽度和高度是EditText对象。输入宽度和高度后,应根据calcArea和calcPerimeter方法实时显示周长和面积的值。用于侦听器的代码是基于我在这里找到的一个示例编写的。我的代码:

代码语言:javascript
复制
    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;
    }
}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-04-25 05:27:26

在onCreate中的编辑文本中定义了这一点。

代码语言:javascript
复制
  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();

        }
    });
票数 6
EN

Stack Overflow用户

发布于 2014-04-24 06:49:05

试试这个密码-

activity_main.xml

代码语言:javascript
复制
<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 -

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-04-24 01:02:22

你能澄清一下你试图在一个侦听器中用两个文本视图完成什么吗?我的第一个建议是创建一个接受文本视图作为参数的回调。然后让您的类接受由这两个类创建的两个回调。不确定这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23257919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档