close

 

(1)Calling one Activity from another in Android
用法:

Intent launchActivity2 = new Intent(Activity1.this, Activity2.class);

 you can simply launch the activity by running:
startActivity(launchActivity2 );

utton next = (Button) findViewById(R.id.TEST);
next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent( view.getContext(), MyActivity.class);
        startActivity(myIntent);
    }
});

參考資料:http://hmkcode.com/android-start-another-activity-within-the-same-application/

(2)Android – Passing Data to Another Activities

(1)MainActivity.java
package com.hmkcode.android;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity implements OnClickListener {
 
    Button btnStartAnotherActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btnStartAnotherActivity = (Button) findViewById(R.id.btnPassData);
 
        btnStartAnotherActivity.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
 
        // 1. create an intent pass class name or intnet action name
        Intent intent = new Intent("com.hmkcode.android.ANOTHER_ACTIVITY");
 
        // 2. put key/value data
        intent.putExtra("message", "Hello From MainActivity");
 
        // 3. or you can add data to a bundle
        Bundle extras = new Bundle();
        extras.putString("status", "Data Received!");
 
        // 4. add bundle to intent
        intent.putExtras(extras);
 
        // 5. start the activity
        startActivity(intent);
    }
 
}
 
(2)AnotherActivity.java
 
package com.hmkcode.android;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
 
public class AnotherActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
 
        // 1. get passed intent
        Intent intent = getIntent();
 
        // 2. get message value from intent
        String message = intent.getStringExtra("message");
 
        // 3. show message on textView
        ((TextView)findViewById(R.id.tvMessage)).setText(message);
 
        // 4. get bundle from intent
        Bundle bundle = intent.getExtras();
 
        // 5. get status value from bundle
        String status = bundle.getString("status");
 
        // 6. show status on Toast
        Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
        toast.show();
    }
}

(3) Define All Activities in the Manifest XML File

<?xml version="1.0" encoding="utf-8"?>
    package="com.hmkcode.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
 
        <activity
            android:name="com.hmkcode.android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <activity
            android:name="com.hmkcode.android.AnotherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.hmkcode.android.ANOTHER_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

參考資料:http://hmkcode.com/android-passing-data-to-another-activities/

(3)Android – Passing Java Object to Another Activity

(3.1) MainActivity.java

package com.hmkcode.android;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity implements OnClickListener {
 
    Button btnPassObject;
    EditText etName, etAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btnPassObject = (Button) findViewById(R.id.btnPassObject);
        etName = (EditText) findViewById(R.id.etName);
        etAge = (EditText) findViewById(R.id.etAge);
 
        btnPassObject.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
 
        // 1. create an intent pass class name or intnet action name
        Intent intent = new Intent("com.hmkcode.android.ANOTHER_ACTIVITY");
 
        // 2. create person object
        Person person = new Person();
        person.setName(etName.getText().toString());
        person.setAge(Integer.parseInt(etAge.getText().toString()));
 
        // 3. put person in intent data
        intent.putExtra("person", person);

        // 4. start the activity
        startActivity(intent);
    }
 
}
 

(3.2) AnotherActivity.java

package com.hmkcode.android;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class AnotherActivity extends Activity {
 
    TextView tvPerson;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
 
        // 1. get passed intent
        Intent intent = getIntent();
 
        // 2. get person object from intent
 
        Person person = (Person) intent.getSerializableExtra("person");
 
        // 3. get reference to person textView
        tvPerson = (TextView) findViewById(R.id.tvPerson);
 
        // 4. display name & age on textView
        tvPerson.setText(person.toString());
 
    }
}
 
(3.3)Define Activities in the Manifest XML File
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hmkcode.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
 
        <activity
            android:name="com.hmkcode.android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <activity
            android:name="com.hmkcode.android.AnotherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.hmkcode.android.ANOTHER_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
 
參考資料:http://hmkcode.com/android-passing-java-object-another-activity/

(4)Android ImageButton Example Code

  1) XML File: activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.faraz.imagebutton_example.MainActivity">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/finger"
        tools:layout_editor_absoluteX="130dp"
        tools:layout_editor_absoluteY="155dp" />
</android.support.constraint.ConstraintLayout>

2) File: MainActivity.java

package com.example.faraz.imagebutton_example;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    ImageButton imageButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        imageButton = (ImageButton) findViewById(R.id.imageButton1);

        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Toast.makeText(MainActivity.this, "You Clicked Image Button!", Toast.LENGTH_LONG).show();

            }

        });

    }

}

參考資料:https://www.includehelp.com/android/ImageButton-Example-Code.aspx

 

(4)

android 的view類的setVisibility();值的意思

android view setVisibility():
有三個參數:Parameters: visibility One of VISIBLE , INVISIBLE , or GONE,想對應的三個常量值:0、4、8
VISIBLE:0 意思是可見的
INVISIBILITY:4 意思是不可見的,但還佔著原來的空間
GONE:8 意思是不可見的,不佔用原來的佈局空間
 
View Code 
 LinearLayout num_Layout = (LinearLayout) findViewById(R.id.num_layout);
  if (numberOfRecord > 0){ 
              TextView num = (TextView) findViewById(R.id.num);
              num.setText(numberOfRecord.toString() );
              num_Layout.setVisibility(0); 
         } 
else {
              num_Layout.setVisibility(8);
         }
 
 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 stanley 的頭像
    stanley

    史坦利Stanley程式Maker的部落格

    stanley 發表在 痞客邦 留言(0) 人氣()