表Android的动态更新值 [英] Dynamically update values in table android

查看:109
本文介绍了表Android的动态更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是,我有一组4的EditText字段(数量)和表布局其中,结果显示在2列。在表布局的值由其中使用的所有的EditText值的公式计算的。我想在表中作为动态更新应答和当的EditText字段我输入值。可能吗?如果是这样,请扔光就可以用样片code的。

The question is, I have a set of 4 edittext fields (number) and a table layout wherein the results are displayed in 2 columns. The values in table layout are computed by a formula where all the edittext values are used. I want to update answer dynamically in the table as and when I input values in edittext fields. Is it possible? If so please throw a light on it with a sample piece of code.

和表在其他活动。

编辑文本code

al_e.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
            if((al<15)|(al>50)|al_e.getText().toString().length()==0){
                invalid=1;
                //al_e.setText(null);
                Toast.makeText(getApplicationContext(),"Please enter a valid input\nRange [15mm,50mm]", Toast.LENGTH_SHORT).show();
            }
        }
    });


屏幕截图。

表布局设置为tabhost的setcontent。

The table layout is set as setcontent of tabhost.

自定义addtab方法tabhost

private void addTab(String label, String tag, Drawable drawable, Intent intent) {
    TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
    spec.setIndicator(createTabIndicator(label, drawable));
    spec.setContent(intent);
    intent.putExtra("AL", al);
    intent.putExtra("K1", k1);
    intent.putExtra("K2", k2);
    intent.putExtra("ALC", al_const);
    intent.putExtra("DR", dr);
    intent.putExtra("INVALID", invalid);
    mTabHost.addTab(spec);
}


其tabhost活动实现了一个


its implements in tabhost activity as

mTabHost=(TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(getLocalActivityManager());
    addTab("SRK/T", TAG_1, createTabDrawable(R.drawable.ic_tab_eye_unselected), new Intent(Second.this, Srkt_x.class));
    addTab("SRK II", TAG_2, createTabDrawable(R.drawable.ic_tab_eye_unselected),new Intent(Second.this, Srk2_x.class));
    addTab("BINKHORST", TAG_3, createTabDrawable(R.drawable.ic_tab_eye_unselected), new Intent(Second.this, Binkhorst_x.class));
    addTab("HOLLADAY", TAG_4, createTabDrawable(R.drawable.ic_tab_eye_unselected), new Intent(Second.this, Holladay_x.class));


表活动是srkt_x,srkt_2,holladay_x,binkhorst_x活动,我设置的内容tabhost

其实我已经实现了标签的的EditText字段和我已经设置了选项卡,表布局的内容。这样,当我更新的EditText领域,我想表领域的布局是其正下方的EditText进行更新。

Actually i have implemented the edittext fields in a tab and i have set the content of the tab as table layout. So as and when i update edittext field i want the table layout fields which is right below edittext to be updated.

PS:原谅坏的图像编辑

推荐答案

您可以使用您的editext的 addTextChangedListener 方法,因为它是由用户更新,以获取其内容。看到该文档,<一个href=\"http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29\"相对=nofollow>这里

You can use the addTextChangedListener method on your editext to get its content as it's updated by the user. See the doc here

样code更新与EDITTEXT输入一个TextView:

Sample code to update a textView with an editText input :

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:ems="10" >

    <requestFocus />
</EditText>

活动:

public class MainActivity extends Activity {

private TextView textView;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     editText= (EditText)findViewById(R.id.editText1);
     textView= (TextView) findViewById(R.id.textView);
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            textView.setText(s); //use the charsequence s which is the current user input

        }

        @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

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

修改
你的表是另一项活动?并且要更新当您使用EDITTEXT监听器甚至不膨胀的表?我想你应该更多地解释,并在这里发表您的所有code。

EDIT Your table is in another activity?? And you want to update a table that is not even inflated when you use your editText listener?? I think you should explain more and post all your code here.

修改您正在使用相同的屏幕,简直是一场相当罕见的两项活动。现在,您可以改用其行为之类的活动,让他们住在同一个画面片段。他们之间很容易在许多方面可以进行沟通pretty,使用getActivity方法。

EDIT You are using two activities in the same screen which is frankly quite rare. You can now instead use Fragments which behave like activities and have them to live in the same screen . You can them communicate between them pretty easily in many ways, using getActivity method.

这篇关于表Android的动态更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆