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) {
Intent intent =
new
Intent(
"com.hmkcode.android.ANOTHER_ACTIVITY"
);
intent.putExtra(
"message"
,
"Hello From MainActivity"
);
Bundle extras =
new
Bundle();
extras.putString(
"status"
,
"Data Received!"
);
intent.putExtras(extras);
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) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
Intent intent = getIntent();
String message = intent.getStringExtra(
"message"
);
((TextView)findViewById(R.id.tvMessage)).setText(message);
Bundle bundle = intent.getExtras();
String status = bundle.getString(
"status"
);
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) {
Intent intent =
new
Intent(
"com.hmkcode.android.ANOTHER_ACTIVITY"
);
Person person =
new
Person();
person.setName(etName.getText().toString());
person.setAge(Integer.parseInt(etAge.getText().toString()));
intent.putExtra(
"person"
, person);
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) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
Intent intent = getIntent();
tvPerson = (TextView) findViewById(R.id.tvPerson);
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():
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);
}