我有一个弹出文本视图,下一个按钮和onClick我想切换内容。 这就是我试过的......无法让它发挥作用。我得到的错误是类型不匹配无法从void转换为int
package com.jibushi;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP=7;
private static final long TIME_DELAY=1000;//1 seconds
private View view;
private Button nextButton;
private Button skipButton;
private TextView nextSwitch;
private int tutStrings;
private int tutStrings2;
private int tutStrings3;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_SHOW_POPUP:
view();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 1 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);
nextSwitch = (TextView) findViewById(R.id.lessonsDialog);
tutStrings = nextSwitch.setText(R.string.lessons_introduction); //type mismatch cannot convert from void to int
tutStrings2 = nextSwitch.setText(R.string.lessons_introduction2); //type mismatch cannot convert from void to int
tutStrings3 = nextSwitch.setText(R.string.lessons_introduction3); //type mismatch cannot convert from void to int
this.nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case tutStrings:
nextSwitch.setText(R.string.lessons_introduction2);
break;
case tutStrings2:
nextSwitch.setText(R.string.lessons_introduction3);
break;
}
}
});
}
private void view() {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
parent.addView(view);
}
public Button getSkipButton() {
return skipButton;
}
public void setSkipButton(Button skipButton) {
this.skipButton = skipButton;
skipButton.findViewById(R.id.skip_button);
}
public Button getNextButton() {
return nextButton;
}
public void setNextButton(Button nextButton) {
this.nextButton = nextButton;
nextButton.findViewById(R.id.next_button);
}
}
是的,缺乏研究。不知道字符串数组......终于搞定了!
package com.jibushi;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP=7;
private static final long TIME_DELAY=1000;//1 seconds
private View view;
private int count = 0;
private TextView lessonsDialog;
private String[] lessonDialogString;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_SHOW_POPUP:
view();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 1 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);
}
private void view() {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
parent.addView(view);
lessonsDialog = (TextView) findViewById(R.id.lessonsDialog);
Resources res = getResources();
myString = res.getStringArray(R.array.lessons_dialog_array);
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (count < lessonDialogString.length) {
lessonsDialog.setText(lessonDialogString[count]);
count++;
}
}
});
}
}
答案 0 :(得分:0)
这里有一些问题。
我不知道你在这里要做什么:
nextSwitch = (TextView) findViewById(R.id.lessonsDialog);
tutStrings = nextSwitch.setText(R.string.lessons_introduction); //type mismatch cannot convert from void to int
tutStrings2 = nextSwitch.setText(R.string.lessons_introduction2); //type mismatch cannot convert from void to int
tutStrings3 = nextSwitch.setText(R.string.lessons_introduction3); //type mismatch cannot convert from void to int
TextView.setText(String)
没有返回值,因此这些行没有任何意义。你是想自己保存字符串吗?
此外,您的onClick
方法也存在问题。传递给View
的{{1}}是点击的实际onClick
,在这种情况下将是view
。在您的代码中,您将切换未定义的整数,而不是您活动中不同视图的ID。
而是做这样的事情:
nextButton
然后,您只需定义void onClick(View v){
switch(v.getId()){
case R.id.next_button:
setTextViewNextString();
break;
case R.id.prev_button:
setTextViewPrevString();
}
和setTextViewNextString()
方法
答案 1 :(得分:0)
方法setText返回void:
public final void setText(int resid)
(来自http://developer.android.com/reference/android/widget/TextView.html#setText%28int%29)
您不能将void分配给int变量。这些行
tutStrings = nextSwitch.setText(R.string.lessons_introduction); //type mismatch cannot convert from void to int
tutStrings2 = nextSwitch.setText(R.string.lessons_introduction2); //type mismatch cannot convert from void to int
tutStrings3 = nextSwitch.setText(R.string.lessons_introduction3); //type mismatch cannot convert from void to int
错了。
我认为你可以用这种方式解决问题:
1)将ID保存在私有变量中:
tutStrings = R.string.lessons_introduction;
tutStrings2 = R.string.lessons_introduction2;
tutStrings3 = R.string.lessons_introduction3;
2)保存字符串,从资源中获取字符串:
private String s1, s2, s3;
...
s1 = this.getString(R.string.lessons_introduction);
s2 = this.getString(R.string.lessons_introduction2);
s3 = this.getString(R.string.lessons_introduction3);
3)现在您可以使用onClick方法:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case tutStrings:
nextSwitch.setText(s2);
break;
case tutStrings2:
nextSwitch.setText(s3);
break;
}
}
答案 2 :(得分:0)
抱歉,我缺乏研究。使用的字符串数组..完美无缺!
答案 3 :(得分:-1)
像这样更改您的代码:
tutStrings = this.getString(R.string.lessons_introduction);
tutStrings2 = this.getString(R.string.lessons_introduction2);
tutStrings3 = this.getString(R.string.lessons_introduction3);
请注意:在onClick
中,您必须将this
替换为您的班级名称,例如Myclass.getString
或仅删除this.