2014-11-06 83 views
1

我在我的應用程序中有一個孩子片段,即時通訊試圖測試功能英寸的想法是,textview將更新模仿任何輸入到edittext。但目前它不喜歡setText,我不知道爲什麼。我檢查過我是否錯過了一個導入,並且我認爲這些小部件是正確定義的。任何關於如何解決這個問題的建議都會很棒。不能解析符號「setText」SherlockActionBar片段

import com.actionbarsherlock.app.SherlockFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.TextView; 

public class WeightFragment extends SherlockFragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_weight, container, false); 
    return rootView; 
} 

EditText weight = (EditText) getActivity().findViewById(R.id.weightInput); 
TextView label = (TextView) getActivity().findViewById(R.id.weightResult); 

String result = weight.getText().toString(); 
label.setText(result); 

} 

回答

0

把代碼中的方法,就像這樣:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_weight, container, false); 

    EditText weight = (EditText) getActivity().findViewById(R.id.weightInput); 
    TextView label = (TextView) getActivity().findViewById(R.id.weightResult); 

    String result = weight.getText().toString(); 
    label.setText(result); 
    return rootView; 
} 
+0

只是嘗試這樣做,它的工作(說不上來爲什麼我沒有嘗試這首先哈哈)。我假設如果我把它放在oncreate它不會更新每次我改變editText中的值。你有什麼想法,我會如何使它動態? – 2014-11-06 20:27:05

+0

只需創建一個方法'updateLabel'並隨時調用它 – rickyalbert 2014-11-06 20:27:58

+0

您能解釋爲什麼它應該放在方法中嗎? – 2016-04-30 19:36:04