1

フラグメントを使用してタブを作成する方法についてのチュートリアルに従っていました。各タブには、Javaファイルに次のものがあります。

パブリッククラスrdfriはフラグメントを拡張します{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist.  The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed.  Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }
   return (LinearLayout)inflater.inflate(R.layout.rdfri, container, false);
}

}

フラグメントでimageButtonを取得してみます。私は画像ボタンがこのコードで動作すると考えました:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rdmon);

ImageButton rainbowbook =(ImageButton)findViewById(R.id.imageButton1);

    rainbowbook.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class);
            rdmon.this.startActivity(myIntent);
        }
    });

}

では、フラグメントコードでボタンコードを取得するにはどうすればよいですか?

ありがとう。

4

1 に答える 1

0

onActivityCreatedフラグメント クラスのオーバーライドされたメソッドにボタン コードを配置します。

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState);   
    ImageButton rainbowbook = (ImageButton) findViewById(R.id.imageButton1);

    rainbowbook.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
        Intent myIntent = Intent(rdmon.this, RainbowBookActivity.class); 
        rdmon.this.startActivity(myIntent); 
       } 
    }); 
}

もちろん、これはイメージボタンがフラグメントレイアウトに含まれていることを前提としています。

于 2012-07-31T16:28:33.343 に答える