Andoid的Button+Intent(適合初學者),andoidintent

來源:互聯網
上載者:User

Andoid的Button+Intent(適合初學者),andoidintent

自己剛學Android的時候在這上面花了不少時間,資料沒少找。學習別人的“關鍵代碼”,自己寫起來不是缺這就是缺那的。希望後來的同學在這上面少浪費些時間。

其實很簡單,就是用一個Android的Intent實現Button的頁面跳轉,單擊圖1的Button,進入圖2


圖1


圖2

首先建立一個Android Application project,主類名為MainActivity不改,sdk沒問題的話系統會自動產生onCreate函數,MainActivity繼承Activity,出現紅燈就import

建立成的project正常會有一句Hello World!在res的layout檔案夾下會有一個xml檔案,版本比較低的是main.xml,比較高的想sdk23.0.2是activity_main.xml,

用來定義布局,把<TextView>..</TextView>相關刪掉(用來顯示Hello World的),加入

 <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="140dp"        android:text="Button" />
在Graphical layout可以先看到定義好的頁面,1,接下來寫圖2的布局,隨便在原目錄layout下建立一個e.xml檔案,布局為LinearLayout,內容如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Textddddddddddddddddd" /></LinearLayout>

寫一個對應圖2的tt類

package com.example.buttontest;import android.app.Activity;import android.os.Bundle;public class tt extends Activity{protected void onCreate(Bundle savedInstanceState)     {       super.onCreate(savedInstanceState);       setContentView(R.layout.e);    }}


MainActivity內容如下

package com.example.buttontest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {Button but=null;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        but=(Button) findViewById(R.id.button1);        but.setOnClickListener(new View.OnClickListener() {  //監聽Buttonpublic void onClick(View arg0) {Intent intent=new Intent();intent.setClass(MainActivity.this, tt.class);startActivity(intent);}                });    }}

可以在onClick函數的最後加上MainActivity.this.finish();,加上的話在圖2介面退出整個系統都退出。沒加的話在圖2退出後還能看到圖1頁面

最後,要在AndroidMainfest.xml裡面定義tt類,不然在模擬器裡面是進入了圖2介面的。主類一般在建立的時候系統自己會定義

<activity android:name=".tt"></activity>



html中的代碼與解釋,適合初學者的代碼

常規的套路結構來說:
<html> //這個是網站的標誌哦
<head> //網站的頭部
<titlle></title> //這裡是網站的標題部分
<link href="css.css" rel="stylesheet" type="text/css"/> //css外部參考
<script src="java.js" type="text/javascript"></script> //javascript外部參考
<script language="javascript">
//javascript通常定義在head,也可以定義在body裡面,還可以外部調入,具體的運行順序可以網上查查
</script>
<style type="text/css">
//css樣式通常定義在head,也可以定義在行內,還可以外部調入,具體的可以網上查查
</style>
</head>
<body>
'這裡是網站的主體部分,通常有表單(form),表格(table),按鈕(button)等等
'(這裡省略n個字),具體可以網上去學學
</body>
</html>
------------------------------------------
推薦網站:www.w3school.com.cn
和www.aa25.cn
可以好好學學哦~包含(html,asp,div+css,javascript,ajax,jQuery等等)足夠你設計一個非常炫的動態網站和靜態網站,對你學網頁設計非常有協助哦~
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.