【问题标题】:Dynamically update values in table android动态更新表android中的值
【发布时间】:2013-02-01 14:09:52
【问题描述】:

问题是,我有一组 4 个编辑文本字段(数字)和一个表格布局,其中结果显示在 2 列中。表格布局中的值由使用所有 edittext 值的公式计算。当我在edittext字段中输入值时,我想在表格中动态更新答案。是否可以?如果是这样,请使用示例代码对其进行说明。

并且该表在另一个活动中。

编辑文本代码

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。


tabhost的自定义addtab方法

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 活动中的实现为

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正下方的表格布局字段。

PS:请原谅糟糕的图片编辑

【问题讨论】:

    标签: android dynamic-data android-tablelayout


    【解决方案1】:

    您可以在editext 上使用addTextChangedListener 方法来获取用户更新的内容。请参阅文档here

    使用 editText 输入更新 textView 的示例代码:

    布局文件:

    <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 侦听器时甚至没有膨胀的表?我认为您应该在这里解释更多并发布您的所有代码。

    编辑 您在同一个屏幕上使用两个活动,坦率地说,这是非常罕见的。您现在可以改为使用行为类似于活动的片段并让它们生活在同一个屏幕中。您可以使用 getActivity 方法以多种方式轻松地在它们之间进行通信。

    【讨论】:

    • 我已经做到了。即使表格没有更新。你能提供一段示例代码吗
    • 我认为您应该先分享您的代码,以便我们看一下;)
    • 如果文本视图在另一个活动中怎么办?
    • 那么你的问题没有任何意义,你不会动态更新屏幕上没有的东西。如果您有一个按钮可以转到您的表格活动,只需使用 putExtra() 函数将表格活动意图传递给您的编辑文本的内容。
    • 现在你说你的桌子就在你的editText下面,当你第一次说它在另一个活动中时......多么令人困惑!如果您需要更多帮助,请发布您的所有代码(布局/片段/活动)并准确说明您想做什么!
    猜你喜欢
    • 2013-11-28
    • 2018-05-25
    • 2013-06-25
    • 1970-01-01
    • 2018-01-29
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多