Dynamically update values in table android

瘦欲@ 提交于 2020-01-24 00:48:07

问题


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.

And the table is in another activity.

edit text 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();
            }
        }
    });

Screenshot of screen.

The table layout is set as setcontent of tabhost.


the custom addtab method for 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);
}

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

The table activity are srkt_x,srkt_2,holladay_x,binkhorst_x activity which i set as content of tabhost

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:pardon for bad image editing


回答1:


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

Sample code to update a textView with an editText input :

Layout file:

<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>

Activity:

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;
}

}

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.

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.



来源:https://stackoverflow.com/questions/14648126/dynamically-update-values-in-table-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!