我堅持一個fragment.below內是我的片段代碼onCreateview的片段設置值全局變量返回空值?
public class Tab1 extends Fragment {
EditText address,contactNumber,dob;
String adds;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
contactNumber= (EditText)v.findViewById(R.id.editTextcontact);
address= (EditText)v.findViewById(R.id.editText2);
dob= (EditText) v.findViewById(R.id.editText3);
contactNumber.setHint("Your ContactNo Plz");
address.setHint("Your address Plz");
dob.setHint("Your Data Of Birth");
//receive data from main activity
TabedActivity activity = (TabedActivity) getActivity();
String myDataFromActivity = activity.getMyData();
//
adds=address.getText().toString();
return v;
}
public String getData(){
return adds;
}
}
我想是時候該片段從上述主要活動堪稱EditText上的文字應該傳遞給加variable.i檢查它像但是它顯示爲空值。我可以設置這個值。?請幫助
你在哪裏/何時在你的Activity中調用getData()? –
@UdiIdan在我的主要活動onOptionItemSelected我稱這個函數是否正確? public void ShoValue(){ Tab1 tab = new Tab1(); addre = tab.getData()。toString(); Toast.makeText(this,「you select done」+ addre,Toast.LENGTH_SHORT).show(); } –
用戶想輸入文本,對不對?所以當你做Tab1 tab = new Tab1(); addre = tab.getData()。的toString();?另外,你在我的回答 –