1

今日は、フラグメント アクティビティを使用してプロジェクトを作成してみます。MainActivity エクステント FragmentActivity があります。MainActivity にはレイアウトがあります。

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000" />

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

レイアウトには 2 つのボタンがあり、ここに Fragment を置き換えるための FrameLayout があります。MainActivity onCreate にフラグメントを挿入します。

MainActivity.java

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.e("Button", "click--------------");
            }
        );
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(null).commit();
    }
}

R.id.frame_layout にフラグメントを挿入すると、button1 をタッチしようとしますが、応答しません。logcat に表示されない

私を助けてください!ありがとう

4

2 に答える 2

1

フラグメントではこれを使用します:

あなたの XML :

  <Button
            android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="yourmethod"
            android:text="Login" />

あなたのコード:

  public void yourmethod(View v){
    //dosomthing 
 }
于 2013-05-30T11:37:24.487 に答える