我有一個程序,其中我需要更新在一個片段作爲值的實時和當它在fragmentactivity.The快照輸入如下所示從fragmentactivity值傳遞實時片段化
這裏因爲當我在Fragmentactivity(圖片的上半部分)中輸入值時,AL,K1等的edittext字段中的答案必須在下面的片段中更新。我應該如何發送值實時分段。幫助我我是一個初學者
我有一個程序,其中我需要更新在一個片段作爲值的實時和當它在fragmentactivity.The快照輸入如下所示從fragmentactivity值傳遞實時片段化
這裏因爲當我在Fragmentactivity(圖片的上半部分)中輸入值時,AL,K1等的edittext字段中的答案必須在下面的片段中更新。我應該如何發送值實時分段。幫助我我是一個初學者
在你創建片段的類中,創建一個名爲public void updateText(String text)
的方法。在此方法中,執行所有計算等,以便在傳入字符串的情況下計算片段中的數字,然後將文本設置爲在所有計算後獲得的字符串。在FragmentActivity
中,使用editText1.addTextChangedListener(this)
並將implements TextWatcher
添加到類聲明中,添加textChangedListener
。添加導入和所有未實現的方法,並在最近添加的onTextChanged
方法中,通過說fragment.updateText(String)
調用updateText(String)
方法,其中fragment是您將它附加到FragmentActivity
時創建的Fragment對象。
我希望這有助於!讓我知道你是否需要別的東西。
或者,您可以更改updateText(String)
方法以將參數添加或更改爲最適合您需要的參數。
下面是一個粗略的例子,希望有助於。
package com.smarticle.example;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class Example extends Activity implements TextWatcher {
ExampleFragment exampleFragment;
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exampleFragment = new ExampleFragment();
editText1 = (EditText) findViewById(R.id.editText1);
editText1.addTextChangedListener(this);
}
@Override
protected void onPause() {
super.onPause();
editText1.removeTextChangedListener(this);
}
public void afterTextChanged(Editable s) {
// I don't implement this, but it is required.
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// I don't implement this, but it is required.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
exampleFragment.updateText(editText1.getText().toString().trim());
}
public class ExampleFragment extends Fragment {
public void updateText(String text) {
// Perform any necessary calculations or actions
}
}
}
可否請您提供一段樣本代碼。我很困惑。 – human 2013-02-12 15:40:13
-1;沒有示例代碼 – AndroidDev 2013-04-17 17:21:29
@ Coder101我添加了一個示例,顯示了我所描述的內容。請刪除您的-1。希望我的例子會有所幫助。 – Adam 2013-04-19 14:28:37
http://stackoverflow.com/questions/5208377/call-fragment-events-from-activity – abc667 2013-02-12 15:05:38
@ abc667,但我想它發送實時 – human 2013-02-12 15:20:15
更重要的是實時不是調用方法並傳遞值?這種行爲的結果是什麼?爲什麼這不符合你的要求? – 2013-02-12 15:25:23