2011-11-22 39 views
-1

我有一个弹出文本视图,下一个按钮和onClick我想切换内容。 这是我试过...不能得到它的工作。我得到的错误是类型不匹配不能转换从无效到int创建一个“下一步”按钮,切换TextView的内容

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方法也有问题。传递给onClickView是实际的view被点击,在这种情况下将是nextButton。在您的代码中,您将切换未定义的整数,而不是您活动中不同视图的ID。

而是做这样的事情:

void onClick(View v){ 

switch(v.getId()){ 
    case R.id.next_button: 
     setTextViewNextString(); 
    break; 
    case R.id.prev_button: 
     setTextViewPrevString(); 
} 

然后你只需定义setTextViewNextString()setTextViewPrevString()方法

-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.

+0

setText方法返回null ...你怎样给null赋值给int变量? –

+0

Sry只是把你的代码写在那里,只是看着setTextMethod而不是你的其他错误。 – Thommy

0

的setText返回void的方法:

公众最终无效的setText(INT渣油)

(从http://developer.android.com/reference/android/widget/TextView.html#setText%28int%29

您不能分配无效到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; 

    } 
}