![](/img/trans.png)
[英]Buttons not displaying properly after upgrade to ADT 20 (Helios, Mac)
[英]Android Buttons in Eclipse Helios (ADT 10.0.1) do not link properly
这是我在该网站上的第一篇文章,我会尽量按照所要求的技巧具体说明。 我正在使用带有 ADT 10.0.1 的 eclipse Helios
我一直在研究 Android 应用程序,它应该是星际争霸 II 的一般指南。 非常基本的东西,它用于编程 class。 它应该有一个带有继续按钮的介绍背景,链接到主菜单。
主菜单包含几个按钮,它们都应该链接到创建的不同布局。 当我在模拟器中启动我的应用程序时(我尝试了 12 级、9 级等),第一个按钮链接到菜单,但菜单上的按钮无法链接。 我的代码中没有语法错误,但是它确实显示了除第一个按钮之外的所有按钮的整个黄色下划线。 我对基本语法进行了一些调整,让它不显示任何红色或黄色下划线,它也没有让我到任何地方。 当我从代码中删除最后一个按钮时,它是布局中的后退按钮,链接回前面的菜单,菜单上的按钮开始链接到最后一个按钮的链接,我的意思是代码中的最后一个. 所以我认为它可能会跳过所有其他侦听器,只使用代码中的最后一个按钮,或者类似的东西。 请记住,我还不太擅长编程。
这是代码的基本外观。
package lol.lol;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ofk extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle button1) {
super.onCreate(button1);
setContentView(R.layout.intro);
Button button = (Button) findViewById(R.id.continuebutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.frontmenu);
}
});
final Button button2 = (Button) findViewById(R.id.aboutapp);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.aboutsc2g);
}
});
final Button button3 = (Button) findViewById(R.id.aboutsc);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.aboutsc2);
// Perform action on click
}
});
final Button button4 = (Button) findViewById(R.id.micro);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.micro);
// Perform action on click
}
});
final Button button5 = (Button) findViewById(R.id.macro);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.macro);
// Perform action on click
}
});
final Button button6 = (Button) findViewById(R.id.mechanics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.mechanics);
// Perform action on click
}
});
final Button button7 = (Button) findViewById(R.id.zergbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.zergbasics);
// Perform action on click
}
});
final Button button8 = (Button) findViewById(R.id.terranbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.terranbasics);
// Perform action on click
}
});
final Button button9 = (Button) findViewById(R.id.protossbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.protossbasics);
// Perform action on click
}
});
final Button button10 = (Button) findViewById(R.id.backbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.frontmenu);
// Perform action on click
}
});
所以我的问题是,如何让布局前菜单上的按钮链接到它们应该链接的位置? 是否有任何语法错误或我需要添加到此代码段以使其正确 function 的内容?
提前致谢!
好吧,让我先说:这不是 android sdk 的意思。 你必须有一个活动来处理一屏内容的每一次变化,这是通过在此处切换内容视图来实现的。 也许看看这里,如果你还没有这样做: http://developer.android.com/guide/topics/fundamentals.html
正确的方式(带有活动),用户可以使用系统后退按钮返回 go,因此基本上不需要后退按钮,用户会期望系统后退按钮像那样工作。 现在,按下系统后退按钮会导致应用程序“关闭”并显示之前打开的内容。
但是,可以像您尝试的那样进行操作。 这是一个简短的片段:
public class Start extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflater mLayoutInflater = getLayoutInflater();
final LinearLayout mLinearLayoutIntro = (LinearLayout) mLayoutInflater.inflate(R.layout.intro, null);
setContentView(mLinearLayoutIntro);
final LinearLayout mLinearLayoutFrontMenu = (LinearLayout) mLayoutInflater.inflate(R.layout.frontmenu, null);
Button continueButton = (Button) findViewById(R.id.continuebutton);
continueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayoutFrontMenu);
}
});
final LinearLayout mLinearLayouAboutSc2g = (LinearLayout) mLayoutInflater.inflate(R.layout.aboutsc2g, null);
final Button aboutAppButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.aboutapp);
aboutAppButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayouAboutSc2g);
}
});
final Button backButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.backbutton);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayoutIntro);
// Perform action on click
}
});
super.onCreate(savedInstanceState);
}
}
你看,你的代码有很大的不同。 您的代码中的第一个错误是将点击侦听器仅绑定到第一个按钮(可能复制/粘贴然后忘记了)。
其次,如果你想这样解决它,你必须手动膨胀你的布局。 否则你会得到 null 指针无处不在,因为这些布局和它的孩子(按钮等)是惰性实例化的(只有在需要它们时,例如在 setcontentview 中调用)。
第三:嗯......真的,为每一屏内容做一个活动:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.