0

問題は、4 つの edittext フィールド (数値) のセットと、結果が 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);
}

タブホスト アクティビティで次のように実装します。

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:下手な画像編集失礼します

4

1 に答える 1

1

addTextChangedListenereditext でメソッドを使用して、ユーザーが更新したコンテンツを取得できます。こちらのドキュメントをご覧ください

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 リスナーを使用するときに、膨張していないテーブルを更新したいですか?? もっと説明して、すべてのコードをここに投稿する必要があると思います。

EDIT同じ画面で 2 つのアクティビティを使用していますが、これは率直に言って非常にまれです。代わりに、アクティビティのように動作するフラグメントを使用して、それらを同じ画面に配置できるようになりました。getActivity メソッドを使用して、さまざまな方法で非常に簡単に通信できます。

于 2013-02-01T14:26:07.723 に答える