Android 创建一个;“下一步”;切换文本视图内容的按钮
我有一个带有下一步按钮的弹出文本视图,单击一次我想切换内容。 这就是我尝试过的…无法让它工作。im获取的错误是类型不匹配无法从void转换为intAndroid 创建一个;“下一步”;切换文本视图内容的按钮,android,Android,我有一个带有下一步按钮的弹出文本视图,单击一次我想切换内容。 这就是我尝试过的…无法让它工作。im获取的错误是类型不匹配无法从void转换为int package com.jibushi; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view
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++;
}
}
});
}
}
package com.jibushi;
导入android.app.Activity;
导入android.content.res.Resources;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.Message;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.TextView;
公共课课时扩展活动{
私有静态最终int消息\u SHOW\u POPUP=7;
专用静态最终长时间延迟=1000;//1秒
私人视野;
私有整数计数=0;
私密文本查看教程对话;
私有字符串[]lessonDialogString;
私有处理程序=新处理程序(){
公共无效handleMessage(消息消息消息){
开关(msg.what){
案例消息\u显示\u弹出窗口:
视图();
打破
}
};
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.lessons);
//这将向处理程序发送一条消息,在1秒后显示弹出窗口。
handler.sendEmptyMessageDelayed(消息显示弹出,时间延迟);
}
私有void视图(){
//TODO自动生成的方法存根
ViewGroup parent=(ViewGroup)findViewById(R.id.lessons\u bg);
view=LayoutInflater.from(getBaseContext()).flate(R.layout.lessons\u对话框,空);
parent.addView(视图);
lessonsDialog=(TextView)findViewById(R.id.lessonsDialog);
Resources res=getResources();
myString=res.getStringArray(R.array.lessons\u对话框\u数组);
按钮nextButton=(按钮)findViewById(R.id.next_按钮);
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
如果(计数
这里有一些问题
我不知道你想在这里做什么:
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(字符串)
不返回值,因此这些行没有任何意义。您正在尝试保存字符串本身吗
您的onClick
方法也有问题。传递给onClick
的视图
是实际单击的视图
,在本例中,该视图将是下一个按钮
。在代码中,您正在打开未定义的int,而不是活动中不同视图的id
相反,你可以这样做:
void onClick(View v){
switch(v.getId()){
case R.id.next_button:
setTextViewNextString();
break;
case R.id.prev_button:
setTextViewPrevString();
}
tutStrings = this.getString(R.string.lessons_introduction);
tutStrings2 = this.getString(R.string.lessons_introduction2);
tutStrings3 = this.getString(R.string.lessons_introduction3);
然后您只需定义
setTextViewNextString()
和setTextViewPrevString()
方法如下更改代码:
void onClick(View v){
switch(v.getId()){
case R.id.next_button:
setTextViewNextString();
break;
case R.id.prev_button:
setTextViewPrevString();
}
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.
方法setText返回void:
公开决赛voidsetText(整数剩余)
(来自)
不能将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;
}
}
对不起,我缺乏研究。使用的字符串数组。。很好用 setText方法返回null。。。如何将空值分配给int变量?Sry只是在那里对代码进行c&p,只是查看setTextMethod,而不是其他错误。