私はアンドロイドには新しいので、フラグメントとその仕組みを学びたいと思っています。長さ変換ツールを作成しようとしましたが、meter
をcentimeters
に変換しました。ボタンのクリックによる断片化
私はアクティビティの2つの部分を持っています.1つはアクティビティレイアウトの一部である2つのedittexts
です。もう1つはfragment
です。
この断片は、基本的にキーパッドと数字と演算子が表示されています。普通のカルシのように。
今私は、フラグメントのライフサイクルについて読み、どのように動作するようになっています。
私がした最初のことは、すべてを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
が不思議な動きをしています。
今、私はアクティビティ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);
}
}
私はこのように私は私のクリックが機能していないようですか?
この問題はどうやって解決できますか?解決策が見つかりません。:
私に簡単に行く、感謝:) 1以下
は私の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以外の別のフラグメントにアクセスすることができます –
これは違いがあるとは思わない –