【问题标题】:How to make text clickable in TextSwitcher so that it can open another activity?如何在 TextSwitcher 中使文本可点击,以便它可以打开另一个活动?
【发布时间】:2014-06-24 09:16:21
【问题描述】:

我的应用中有一个文本切换器。一切正常。我的文本正在切换,但我想在单击文本切换器中的文本时打开另一个活动。我怎样才能在android中实现这一点?

提前谢谢:)

文本切换器的代码是...

public class Course1 extends Activity
{
    private TextSwitcher mSwitcher;
    Button btnNext;

    // Array of String to Show In TextSwitcher
    String textToShow[]={"Main HeadLine","Your Message","New In Technology","New Articles","Business News","What IS New"};
    int messageCount=textToShow.length;
    // to keep current Index of text
    int currentIndex=-1;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
                super.onCreate(savedInstanceState);

              setContentView(R.layout.course1);                       

                // get The references
                btnNext=(Button)findViewById(R.id.buttonNext);
                mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);

                // Set the ViewFactory of the TextSwitcher that will create TextView object when asked
                mSwitcher.setFactory(new ViewFactory() {

                    public View makeView() {
                        // TODO Auto-generated method stub
                        // create new textView and set the properties like clolr, size etc
                        TextView myText = new TextView(Course1.this);
                        myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                        myText.setTextSize(36);
                        myText.setTextColor(Color.BLUE);
                        return myText;
                    }
                });

                // Declare the in and out animations and initialize them 
                Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
                Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

                // set the animation type of textSwitcher
                mSwitcher.setInAnimation(in);
                mSwitcher.setOutAnimation(out);




                // ClickListener for NEXT button
                // When clicked on Button TextSwitcher will switch between texts
                // The current Text will go OUT and next text will come in with specified animation
                btnNext.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        currentIndex++;
                        // If index reaches maximum reset it
                        if(currentIndex==messageCount)
                            currentIndex=0;
                        mSwitcher.setText(textToShow[currentIndex]);
                    }
                });

    }
}

【问题讨论】:

    标签: android textview context-switching switchers


    【解决方案1】:

    试试这个

    mSwitcher.setOnClickListener(new View.OnClickListener() {
    
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            if(currentIndex==0){
                            Intent i = new Intent(getApplicationContext(),NextActivity.class);
                            startActivity(i);
                            } else if(currentIndex==1){
                             Intent i = new Intent(getApplicationContext(),NextActivity.class);
                             startActivity(i);
                            }
                        }
                    });
    

    【讨论】:

    • 我想根据文本打开活动...我不想为每个文本打开相同的活动我认为它会为每个文本打开相同的活动?
    • 然后在onClick方法中获取文本并打开你想要的具体活动
    • 嘿,请你给我的答案投票,以便其他用户可以找到两个可行的答案。
    【解决方案2】:

    我在 shylendra 的帮助下解决了这个问题 :) thanxx :)

    这是我的最终代码......

    btnNext.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
            currentIndex++;
            // If index reaches maximum reset it
            if(currentIndex==messageCount)
                currentIndex=0;
            mSwitcher.setText(textToShow[currentIndex]);
        }
    });
    
    
    mSwitcher.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View v) {
            if(currentIndex==0)
            {
            Intent i = new Intent(getApplicationContext(),lastyear.class);
            startActivity(i);
        }else if(currentIndex==1)
        {
             Intent i = new Intent(getApplicationContext(),MainActivity.class);
             startActivity(i);
    
        }
    
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多