另一种布局中的项目不响应用户事件

时间:2013-07-19 09:13:10

标签: java android eclipse events onclicklistener

请查看以下代码

game_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Game" >

    //Please note the GUI Has been removed


        <include layout="@layout/common_status_bar"/>


</RelativeLayout>

Game.java

package game.games;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class Game extends Activity {


    private ImageView goToLanguageSelection;
    private ImageView goToInternet;
    private Button giveUp;


    private LayoutInflater layoutInflater;
    private View commonView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        commonView = layoutInflater.inflate(R.layout.common_status_bar, null);

        //Intializing instance variables
        goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
        goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
        giveUp = (Button)commonView.findViewById(R.id.giveUpButton);

        flag = getIntent().getBooleanExtra("LANGUAGE", true);

        //Add listeners

        goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.game, menu);
        return true;
    }








    //Will get activated when the ThunderBolt image is clicked  
    private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        }

    };


}

common_status_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#373734"
    android:orientation="horizontal" >



        <ImageView
            android:id="@+id/backToLanguageSelectionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="15dp"
            android:paddingTop="5dp"
            android:src="@drawable/thunderbolt" />

        <ImageView
            android:id="@+id/internetButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:src="@drawable/globe_small_2" />

        <ImageView
            android:id="@+id/justForFun"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="5dp"
            android:paddingTop="5dp" />

         <Button
            android:id="@+id/giveUpButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:text="Button" />


</LinearLayout>

此处, common_status_bar.xml 是一种常见布局。我已通过game_activity.xml将此布局添加到<include layout="@layout/common_status_bar"/>

它显示一切正常,没有问题。但情况是我无法使其中的任何元素起作用。我已将OnClickListener附加到goToLanguageSelection的第一个元素,但它没有响应Click。我通过将click事件添加到所有其他按钮,图像(一次一个)来测试这个,我得到了相同的结果。它没有响应用户点击,或其他什么。

为什么单独布局中的此按钮和图像不响应用户事件?

1 个答案:

答案 0 :(得分:1)

不应再次创建commonView

如果你想保留common_status_bar.xml的引用,你可以用id命名,如下面的代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <include
            android:id="@+id/common_status_bar"
            layout="@layout/common_status_bar" />

    </RelativeLayout>

然后,更改此代码:

       commonView = layoutInflater.inflate(R.layout.common_status_bar, null);

为:

       commonView = findViewById(R.id.common_status_bar);