陆志仁 發表於 2020-3-1 09:45:00

Android开发——三种活动跳转方式

<p style="text-align: center"><span style="font-size: 18px"><strong>Android开发——三种活动跳转方式</strong></span></p>
<p style="text-align: left">&nbsp;</p>
<p style="text-align: left"><strong>1. 点击控件跳转</strong></p>
<p><span style="font-size: 14px">这里用 Button 举例,在布局文件中创建 Button 按钮,在再源码文件中写入活动跳转代码:</span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>Button button1 =<span style="color: rgba(0, 0, 0, 1)"> (Button)findViewById(R.id.button1);
button1.setOnClickListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
            Toast.makeText(MainActivity.</span><span style="color: rgba(0, 0, 255, 1)">this</span>,"跳转界面"<span style="color: rgba(0, 0, 0, 1)">,Toast.LENGTH_SHORT).show();
            Intent intent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,RelativeLayout.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
            startActivity(intent);
      }
});</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 14px">其中Intent函数的使用规则为Intent(当前活动,要跳转活动);</span></p>
<p><span style="font-size: 14px">Toast.makeText()函数的作用是在跳转时出现提示信息,Toast.LENGTH_SHORT为默认的消息出现时间,有两个默认的值:LENGTH_LONG(长)、LENGTH_SHORT(短)。</span></p>
<p><strong><span style="font-size: 14px">2. Handler方式自动跳转</span></strong></p>
<p><span style="font-size: 14px">在源码文件中写入活动跳转代码:</span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">new</span> Handler().postDelayed(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
       @Override
       </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            startActivity(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,RelativeLayout.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">));
      }
},</span>1000);</pre>
</div>
<p>&nbsp;</p>
<p>其中1000代表1000毫秒,这段代码的意思是当进入MainActivity活动后会自动在1000毫秒后跳转至RelativeLayout活动。</p>
<p><strong><span style="font-size: 14px">3. 定时器实现自动跳转</span></strong></p>
<p><span style="font-size: 14px">在源码文件中写入活动跳转代码:</span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>Timer timer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Timer();
TimerTask task </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TimerTask() {
      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            startActivity(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Intent(MainActivity.<span style="color: rgba(0, 0, 255, 1)">this</span>,RelativeLayout.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">));
         }
};
timer.schedule(task,</span>1000,10000);</pre>
</div>
<p>&nbsp;</p>
<p>timer.schedule(task,1000,10000) 的含义是延迟1000毫秒后,执行第一次task,然后每隔10000毫秒执行一次task。</p>
<pre><em id="__mceDel"><span style="font-size: 14px">&nbsp;</span></em></pre>
<p>&nbsp;</p>
<pre><span>&nbsp;</span></pre><br><br>
来源:https://www.cnblogs.com/mulin1999/p/12388414.html
頁: [1]
查看完整版本: Android开发——三种活动跳转方式