gpt4 book ai didi

android - 单击按钮时 setContentView 不起作用

转载 作者:行者123 更新时间:2023-11-29 16:04:02 26 4
gpt4 key购买 nike

请帮助我编写代码。

当我单击任何按钮时,button1、button2 或 button3 打开新 Activity ,但布局是空的,没有任何文本和其他内容。

调用新 Activity 的 Activity 代码:

package com.novator.inweld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TentsCatalog extends Activity implements OnClickListener
{
private Button button1;
private Button button2;
private Button button3;

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

button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);

button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}

@Override
public void onClick(View view)
{
if(view == button1)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "1");
startActivity(intent);
}

if(view == button2)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "2");
startActivity(intent);
}

if(view == button3)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "3");
startActivity(intent);
}
}
}

我的新 Activity 代码:

package com.novator.inweld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

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

Intent intent= getIntent();
String tentId = intent.getStringExtra("tentId");

if(tentId == "1")
{
setContentView(R.layout.tent1);
}

if(tentId == "2")
{
setContentView(R.layout.tent2);
}

if(tentId == "3")
{
setContentView(R.layout.tent3);
}
}

}

最佳答案

使用 equals(),而不是 ==

if(tentId.equals("1"))

关于android - 单击按钮时 setContentView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21075143/

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