나는 안드로이드에 익숙하지 않기 때문에 조각을 배우려고 노력하고 있습니다. 길이 변환기를 만들려고 노력했습니다. meter
을 centimeters
으로 변환합니다. 간단합니다.버튼 클릭으로 단편
이제 활동의 두 부분, 즉 활동 레이아웃의 일부인 edittexts
과 나머지 하나는 fragment
입니다.
이 부분은 기본적으로 키패드, 즉 Numbers와 연산자가 표시되어 있습니다. 정상적인 칼슘처럼.
는 지금은 조각의 수명주기에 대해 읽고이 생각하는 방법을 작동 할 수 있습니다.
그래서 내가 한 첫 번째 일은 모든 것을 onCreateView
방법에 넣는 것이 었습니다.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
getRootView=inflater.inflate(R.layout.calci_keyboard,container,false);
GridLayout gridLayout=(GridLayout) getRootView.findViewById(R.id.calciKeyboardGrid);
for(int i=0;i<gridLayout.getChildCount();i++){
b=(Button)gridLayout.getChildAt(i);
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
b.setOnClickListener(this);
}
return getRootView;
}
것은 클릭 이벤트가 작동한다는 것입니다하지만 edittext
settext
은 작동하지 않습니다. Edittexts
이 이상하게 행동합니다.
이제는 Activity UI에 액세스하고 있다고 생각하여이 부분을 onActivityCreated
함수 내에서 수행해야한다고 생각한 것을 제거하려면이 방법도 시도해 보았습니다.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
getRootView=inflater.inflate(R.layout.calci_keyboard,container,false);
return getRootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@SuppressLint("InflateParams") View getView=(GridLayout) LayoutInflater.from(getActivity()).inflate(R.layout.calci_keyboard,null,false);
GridLayout gridLayout=(GridLayout) getView.findViewById(R.id.calciKeyboardGrid); // i Logged this obj and it was there
for(int i=0;i<gridLayout.getChildCount();i++){
b=(Button)gridLayout.getChildAt(i);
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
b.setOnClickListener(this);
}
}
내가 이런 식으로 작업 할 때 내 클릭이 제대로 작동하지 않는 것 같습니다.
이 문제는 어떻게 처리해야합니까? 해결책을 찾을 수 없습니다. :
한 다음내 onClick 이벤트를 보여줍니다 감사합니다 :) 나를 쉽게 이동합니다.
이제public void onClick(View view) {
View focussedChild=getActivity().getCurrentFocus();
switch (view.getId()){
case R.id.calciKeyboardNine:{
if (focussedChild instanceof EditText) {
firstPart=new StringBuilder("");
secondPart=new StringBuilder("");
EditText editText=(EditText)focussedChild;
if(focussedChild.getId()==R.id.lengthConverterFirst){
if(!TextUtils.isEmpty(firstPart.toString()))
firstPart.replace(0,firstPart.length()-1,editText.getText().toString()+"9");
firstPart.append("9");
editText.setText(firstPart.toString());
editText.setSelection(editText.length());
int a=Integer.parseInt(firstPart.toString());
a=a*100;
editText=getActivity().findViewById(R.id.lengthConverterSecond);//second edit text
editText.setText(Integer.toString(a));
editText.setSelection(editText.length());
}else if(focussedChild.getId()==R.id.lengthConverterSecond){
if(!TextUtils.isEmpty(secondPart.toString()))
secondPart.replace(0,secondPart.length()-1,editText.getText().toString()+"9");
secondPart.append("9");
editText.setText(secondPart.toString());
editText.setSelection(editText.length());
double a=Integer.parseInt(firstPart.toString());
a=a/100;
editText=getActivity().findViewById(R.id.lengthConverterFirst);//first edit text
editText.setText(Double.toString(a));
editText.setSelection(editText.length());
}
}
}
}
}
'getActivity()'를 사용하여 여전히 액세스 할 수 있습니까? \t 왜 정적으로 만들까요? –
예 getActivity()를 사용하여 액세스 할 수 있지만 정적으로 만들면 mainActivity 외부의 다른 조각에 액세스 할 수 있습니다 –
아무런 차이가 없다고 생각합니다. –