安卓入門 使用android建立一個項目 從啟動activity中響應按鈕事件 啟動另一個activity 並傳遞參數

來源:互聯網
上載者:User

標籤:dma   scroll   ted   完成   log   mil   relative   nba   響應   

 啟動android studio建立一個新項目
  public  void  sendMessage(View view){        Intent intent=new Intent(this,DispalyMessageActivity.class);        EditText text =(EditText) findViewById(R.id.edit_message);        String message=text.getText().toString();        intent.putExtra(EXTRA_MESSAGE,message);        startActivity(intent);    }

  添加按鈕響應函數 sendMessage  view中綁定的響應函數必須為公開無傳回值 帶有一個View參數的函數 通過View注入可以拿到View上的資料

Intent 對象常用語啟動一個新的activity 

findViewById 通過view中的id擷取到控制項對象 這與js擷取input類似

通過getText 擷取到輸入框常值內容

intent.putExtra 將資料儲存到intent對象中 這裡EXTRA_MESSAGE 是當前類中定義的一個常量

startActivity用語啟動一個activity 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:showIn="@layout/activity_my"    android:id="@+id/content">    <EditText        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:hint="@string/edit_message"        android:id="@+id/edit_message"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button_message"        android:onClick="sendMessage"/></LinearLayout><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:showIn="@layout/activity_my"android:id="@+id/content"><EditText    android:layout_width="0dp"    android:layout_weight="1"    android:layout_height="wrap_content"    android:hint="@string/edit_message"    android:id="@+id/edit_message"/><Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_message"    android:onClick="sendMessage"/></LinearLayout>

啟動效果 這時點擊send將跳轉到一個新的activity

 

添加一個activity

在myactivity下添加一個class並且繼承AppCompatActivity

重寫 onCreate方法

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.display_message);        getSupportActionBar().setDisplayHomeAsUpEnabled(true);        Intent intent = getIntent();        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);        TextView textView = new TextView(this);        textView.setTextSize(80);        textView.setText(message);        textView.setWidth(100);        RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);        layout.addView(textView);    }

  這通過getIntent擷取到intent對象

擷取其中儲存的message資料

建立一個TextView對象

通過id擷取到View 並向該View動態添加一個textView空間

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/content"    android:layout_width="match_parent"    android:layout_height="match_parent">    </RelativeLayout>
在AndroidManifest(清單)中註冊該View
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.xuelei.myapp">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity            android:name=".MyActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name="com.example.xuelei.myapp.DispalyMessageActivity"            android:label="@string/title_activity_display_message"            android:parentActivityName="com.example.xuelei.myapp.MyActivity" >            <meta-data                android:name="android.support.PARENT_ACTIVITY"                android:value="com.example.xuelei.myapp.MyActivity"/>        </activity>    </application></manifest>

添加一個activity name 該class的完全限定名 包名+類名

lable 設定該activity顯示的lable名稱

parentActivityName 指定父視窗對象  包名+類名
meta-data  這個名字值是額外的任意的可以提供給父組件的資料。一個組件元素能夠包含任意數量的meta-data子項目。它們所有的值都會被收集在Bundle對象中並且使其可以作為組件的 PackageItemInfo.metaData 欄位。
     一般的值可以通過value屬性來指定,但是如果要指定一個資源id作為一個值,那麼就要用resource屬性來代替。在這裡用於擷取 myactivity這個對象
這樣就完成一個activity的跳轉了



 

安卓入門 使用android建立一個項目 從啟動activity中響應按鈕事件 啟動另一個activity 並傳遞參數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.