Java OnClick侦听器未按预期更改文本
我试图在mAssistUpdateButton和mReadAgainButton上使用onclick侦听器,但是这两个选项似乎都不会改变可见文本 任何关于我可能做错了什么的建议,我们都非常感激 资料来源:Java OnClick侦听器未按预期更改文本,java,android,onclick,onclicklistener,Java,Android,Onclick,Onclicklistener,我试图在mAssistUpdateButton和mReadAgainButton上使用onclick侦听器,但是这两个选项似乎都不会改变可见文本 任何关于我可能做错了什么的建议,我们都非常感激 资料来源: public void onClick(View v) { if (v == mAssistUpdateButton) { // Update button for ICS and up is selected // Get the Text
public void onClick(View v) {
if (v == mAssistUpdateButton) {
// Update button for ICS and up is selected
// Get the TextView in the Assist Update UI
TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
String text = "";
CharSequence styledText = text;
switch(v.getId()) {
//case R.id.read_again_btn {
case 0:
// Retrieve the instruction string resource corresponding the
// 2nd set of instructions
text = String.format(getString(R.string.apn_app_text_instr),
TotalSteps);
styledText = Html.fromHtml(text);
// Update the TextView with the correct set of instructions
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 1:
text = getString(R.string.apn_app_text_instr2);
styledText = Html.fromHtml(text);
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 2:
// Final set of instructions-Change to the corresponding layout
setContentView(R.layout.assist_instructions);
String assistUpdateInstr = String.format(
getString(R.string.apn_app_text_instr3), TotalSteps);
styledText = Html.fromHtml(assistUpdateInstr);
TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
assistInstrText.setText(styledText);
mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistInstrButton.setOnClickListener(this);
mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
mReadAgainButton.setOnClickListener(this);
}// end switch
}// end if
else if (v == mAssistInstrButton) {
// "LET'S DO THIS" Button in final instructions screen for ICS and
// up is selected
Values = getValues();
startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}// end if
else if (v == mAssistInstrButton) {
startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}// end if
else if (v == mReadAgainButton) {
// go back to set of instructions if read again is selected
TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
String text = "";
CharSequence styledText = text;
switch (mInstructionNumber) {
case 0:
// Retrieve the instruction string resource corresponding the
// 2nd set of instructions
text = String.format(getString(R.string.apn_app_text_instr),
TotalSteps);
styledText = Html.fromHtml(text);
// Update the TextView with the correct set of instructions
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 1:
text = getString(R.string.apn_app_text_instr2);
styledText = Html.fromHtml(text);
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 2:
// Final set of instructions-Change to the corresponding layout
setContentView(R.layout.assist_instructions);
String assistUpdateInstr = String.format(
getString(R.string.apn_app_text_instr3), TotalSteps);
styledText = Html.fromHtml(assistUpdateInstr);
TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
assistInstrText.setText(styledText);
mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistInstrButton.setOnClickListener(this);
mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
mReadAgainButton.setOnClickListener(this);
break;
}// end switch
}
}
v.getId()
返回传递给onClick()的视图(v
)的id
您正在检查视图的id
是否为0,1,2等
case 0:
您的案例
语句应该是视图的id
case R.id.mAssistUpdateButton:
// do your work if that button was clicked
我不确定你期望他们是什么,但请检查一下
编辑
既然您已经解释了要执行的操作,那么就可以创建一个作为计数器的成员变量,并将其声明为int
。然后您可以保留case
语句,但将开关更改为
switch (counterVariable){
请阅读我发布的链接,以便理解switch
语句,但您要做的是将switch
中的变量与case
选项进行比较,以确定要运行的代码块
显然,每次输入onClick()
使用按钮id而不是0,1,2,…,您都希望将计数器变量增加一,。。。在switch case中,要根据按钮clickWendy执行代码,您的案例必须等于视图ID的值。而不是案例0,它应该是案例R.ID.your_按钮。在mAssistUpdateButton
的if条件内,您正在使用0
、1
和2
的案例打开视图ID。但是,视图ID永远不会有这些值。其他按钮似乎也有类似的逻辑,尽管开关发生在mInstructionNumber
上。我不知道你想用它做什么,但一旦它超过2
,它就变得无用了(至少,我看不到它在任何地方被递减)。在案例R.id.assist\u update\u btn:之后的第二个案例中,我应该使用什么?当我尝试在整个序列中多次使用时,会出现重复的大小写错误(我需要多次单击它,每次都显示新文本)“Ohhhhhh Ohhhh它是maaagic…”)P.s.我最初尝试使用此方法,它用于更改大小写,直到达到“mReadAgainButton”然后onclick侦听器在那里失败了。。。(你也在帮我——知道我在那个实现中做错了什么吗?)。您需要设置断点,并查看其确切的停止位置。正如我的评论所说,我最初的猜测是,TextView
没有设置为clickable。或者您的OnClickListener
设置不正确。它在XML中设置为可单击。。。你看到我的onclick监听器有什么不正确的地方吗?我使用了:mReadAgainButton=(TextView)findviewbyd(R.id.read\u再次\u btn);mReadAgainButton.setOnClickListener(this);