gpt4 book ai didi

java - 其他布局中的项目不响应用户事件

转载 作者:行者123 更新时间:2023-12-01 14:19:29 27 4
gpt4 key购买 nike

请看下面的代码

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但它没有响应单击。我通过将单击事件添加到所有其他按钮、图像(一次一个)来测试这一点,得到了相同的结果。它不响应用户单击或其他任何操作。

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

最佳答案

不应再次创建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);

关于java - 其他布局中的项目不响应用户事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com