2017-12-01 13 views
0

私はアンドロイドには新しいので、フラグメントとその仕組みを学びたいと思っています。長さ変換ツールを作成しようとしましたが、metercentimetersに変換しました。ボタンのクリックによる断片化

私はアクティビティの2つの部分を持っています.1つはアクティビティレイアウトの一部である2つのedittextsです。もう1つはfragmentです。

この断片は、基本的にキーパッドと数字と演算子が表示されています。普通のカルシのように。

enter image description here

enter image description here

今私は、フラグメントのライフサイクルについて読み、どのように動作するようになっています。

私がした最初のことは、すべてを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; 
} 

事は、クリックイベントが働く、ということですが、edittextsettextが動作するようには思えません。 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()); 

        } 
       } 
      } 
     } 
    } 

答えて

0

、私はあまりにもこれを試してみました、私は活動のUIがアクセスしていますが思ったので、私はonActivityCreated関数内でこれを行う必要があり、そうすることを削除する - onActivityCreatedは()メソッドであるため、それは動作しませんでしたフラグメントの活性ではない。

これを試してください - あなたのエディットテキストをアクティビティ内で静的にしてから、MainActivity.editText()のようなアクティビティの名前でフラグメントにアクセスしてください。これが助けてくれるといいですか?

+0

私はまだ 'getActivity()'を使ってアクセスできますか? \t なぜ静的にするのですか? –

+0

はい、getActivity()を使用してそれらにアクセスできますが、静的にするとmainActivity以外の別のフラグメントにアクセスすることができます –

+0

これは違いがあるとは思わない –

関連する問題