我目前正在记事本中处理此问题,但错误仍在继续。我的代码出了什么问题?我正在尝试使用onclick按钮处理带有对象阵列的交通信号灯。
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;`
public class MainActivity extends AppCompatActivity {`
private EditText source;
private EditText translation;
private Button translate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Translate.setClientId("myId");
Translate.setClientSecret("mySecret");
source = (EditText)findViewById(R.id.text_source);
translation = (EditText)findViewById(R.id.text_translated);
translate = (Button) findViewById(R.id.translate);
translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(source.getText().length() == 0){
Toast.makeText(MainActivity.this, "Please enter a word or a phrase to translate...", Toast.LENGTH_SHORT).show() ;
return;
}
else {
String word = source.getText().toString() ;
try {
String translatedText = Translate.execute(word, Language.ENGLISH, Language.ARABIC) ;
//System.out.println( translatedText);
translation.setText(translatedText);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "flop...", Toast.LENGTH_LONG).show() ;
}
}
}
});
}
}
答案 0 :(得分:3)
行。让我们逐行完成:
<html>
<head>
<body>
为什么要在<body>
代码中添加<head>
代码?适当的HTML结构更像是这样:
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
下一步:
<div style="background:black;width:75px;height:150px;margin:auto;">
内联样式难以阅读且难以维护。看看学习CSS。
<div id="redlight" style="background:#ff0000;width:40px;height:40px;border- radius:40px;margin:auto;"></div>
<div id="yellowlight" style="background:#3F4A00;width:40px;height:40px;border-radius:40px ;margin:auto"></div>
<div id="greenlight" style="background:#044A00;width:40px;height:40px;border-radius:40px;margin:auto"></div>
<button onclick="start()" style="width:75px;height:30px;margin:auto">Motion- Start!</button>
再次 - 内联样式。对于这类事情,CSS类是制作。
内联点击处理程序。不是最好的主意,但就目前而言,我们会说没关系。我们有更大的问题需要担心!
现在,<script>
代码:
var redlight = document.GetElementById(redlight);
JavaScript区分大小写。您正在寻找getElementById
。此外,您正在尝试获取ID为redlight
的元素,因此您需要传入字符串,而不是变量:getElementById("redlight")
var yellowlight = document.GetElementById[yellowlight];
var greenlight = document.GetElementById[greenlight];
这里有类似的情况,但还有一件事:用括号调用函数,而不是括号。
var colors = ['#ff0000', '#520202', '#ffff00', '#3F4A00', '#008000', '#044A00']
在该行的末尾缺少分号。
function start()
函数需要用大括号包围:{}
if redlight.style.background == colors[0] {
如果声明条件需要括起来。
另外,如果您使用的是CSS类,那么这将更容易。
redlight.style.background = colors[1] //switch off red
yellowlight.style.background = colors[2] //switch on yellow
} else if (yellowlight.style.background == "yellow") {
yellowlight.style.background = colors[3] //switch off yellow
greenliight.style.background = color[4] //switch on green
} else if (greenlight.style.background == "green") {
greenlight.style.background = colors[5] //switch off green
redlight.style.background = colors[0] //switch on red
}
其余部分:请请, 请 使用课程。他们会让你的生活变得更轻松。也使用分号。他们很重要。
只是......编码时要小心。
以下是您的代码的主要修订版(并且正在运行!):
var redlight = document.getElementById("redlight");
var yellowlight = document.getElementById("yellowlight");
var greenlight = document.getElementById("greenlight");
function start() {
if (redlight.classList.contains("on")) {
redlight.classList.remove("on"); //switch off red
yellowlight.classList.add("on"); //switch on yellow
} else if (yellowlight.classList.contains("on")) {
yellowlight.classList.remove("on"); //switch off yellow
greenlight.classList.add("on"); //switch on green
} else if (greenlight.classList.contains("on")) {
greenlight.classList.remove("on"); //switch off green
redlight.classList.add("on"); //switch on red
}
}
.traffic-light {
background: black;
width: 75px;
height: 150px;
margin: auto;
}
.light {
width: 40px;
height: 40px;
border-radius: 40px;
margin: auto;
}
.light.red {
background: #520202;
}
.light.red.on {
background: #ff0000;
}
.light.yellow {
background: #3F4A00;
}
.light.yellow.on {
background: #ffff00;
}
.light.green {
background: #044A00;
}
.light.green.on {
background: #008000;
}
button {
width: 75px;
height: 30px;
margin: auto;
}
<div class="traffic-light">
<div id="redlight" class="light red on"></div>
<div id="yellowlight" class="light yellow"></div>
<div id="greenlight" class="light green"></div>
<button onclick="start()">Motion- Start!</button>
</div>
从积极的方面来说,跟上评论!即使你的代码中的小评论也比没有更好。
您可能需要考虑使用记事本以外的其他内容进行编码 - 即使是原始代码编辑器也应支持括号匹配。
答案 1 :(得分:1)
您是否尝试在括号中定义功能?
Xcode Version 7.3.1 (7D1014)
另外,小心你的大写字母。语法例如是:
function start()
{
if (redlight.style.background==colors[0])
{
redlight.style.background= colors[1]//switch off red
yellowlight.style.background=colors[2]//switch on yellow
} else if (yellowlight.style.background=="yellow") {
yellowlight.style.background=colors[3]//switch off yellow
greenlight.style.background=color[4]//switch on green
} else if (greenlight.style.background=="green") {
greenlight.style.background= colors[5]//switch off green
redlight.style.background=colors[0]//switch on red
}
}
要完成,你写了#34; greenliight&#34;而不是&#34; greenlight&#34;。
答案 2 :(得分:1)
实际上有很多错误,你去了我会把它分解,但首先是
<强> TL; DR 强>
这是工作代码:
<html>
<body>
<div style="background:black;width:75px;height:150px;margin:auto;">
<div id ="redlight" style="background:#ff0000;width:40px;height:40px;border- radius:40px;margin:auto;"></div>
<div id = "yellowlight" style="background:#3F4A00;width:40px;height:40px;border-radius:40px ;margin:auto"></div>
<div id = "greenlight" style="background:#044A00;width:40px;height:40px;border-radius:40px;margin:auto"></div>
<button onclick="start()" style="width:75px;height:30px;margin:auto">Motion- Start!</button>
</div>
<script>
var redlight = document.getElementById('redlight');
var yellowlight = document.getElementById('yellowlight');
var greenlight = document.getElementById('greenlight');
var colors = ["rgb(255, 0, 0)",'rgb(82, 2, 2)','rgb(255, 255, 0)','rgb(63, 74, 0)','rgb(0, 128, 0)','rgb(4, 74, 0)'];
function start() {
if (redlight.style.backgroundColor == colors[0])
{
redlight.style.backgroundColor = colors[1]; //switch off red
yellowlight.style.backgroundColor = colors[2]; //switch on yellow
}
else if (yellowlight.style.backgroundColor == colors[2]) {
yellowlight.style.backgroundColor = colors[3]; //switch off yellow
greenlight.style.backgroundColor = colors[4]; //switch on green
}
else if (greenlight.style.backgroundColor == colors[4]) {
greenlight.style.backgroundColor = colors[5]; //switch off green
redlight.style.backgroundColor = colors[0]; //switch on red
}
}
</script>
</body>
首先,GetElementById
不起作用,您必须以getElementById
其次,你需要在你的函数{}
start
)
第三,whatch out,style.background返回rgb中的颜色。所以,我更改了颜色数组中的值,将它们放在rgb中。
第四,注意你的变量名,你犯了很多错误。 greenlight
被命名为greenliight
一次。此外,您将colors
数组color
命名为。
比较我的代码和您的代码并进行修复。祝你有个美好的一天
答案 3 :(得分:0)
调用GetElementById时,请务必使用括号而不是方括号。此外,'G'应该是小写的,你需要在元素名称周围加引号。这三行应该是这样的:
document.getElementById("redlight");
答案 4 :(得分:0)
input
var redlight = document.getElementById("redlight");
var yellowlight = document.getElementById("yellowlight");
var greenlight = document.getElementById("greenlight");
function start() {
if (redlight.classList.contains("on")) {
redlight.classList.remove("on"); //switch off red
yellowlight.classList.add("on"); //switch on yellow
} else if (yellowlight.classList.contains("on")) {
yellowlight.classList.remove("on"); //switch off yellow
greenlight.classList.add("on"); //switch on green
} else if (greenlight.classList.contains("on")) {
greenlight.classList.remove("on"); //switch off green
redlight.classList.add("on"); //switch on red
}
}
.traffic-light {
background: black;
width: 75px;
height: 150px;
margin: auto;
}
.light {
width: 40px;
height: 40px;
border-radius: 40px;
margin: auto;
}
.light.red {
background: #520202;
}
.light.red.on {
background: #ff0000;
}
.light.yellow {
background: #3F4A00;
}
.light.yellow.on {
background: #ffff00;
}
.light.green {
background: #044A00;
}
.light.green.on {
background: #008000;
}
button {
width: 75px;
height: 30px;
margin: auto;
}