Activity啓動
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=getIntent();
textView= (TextView) findViewById(R.id.textView);
textView.setText(intent.getStringExtra(Config.INTENT_TO));
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Second_Activity.class);
startActivityForResult(intent);
}
});
}
public class MainActivity extends Activity {
private TextView textView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=getIntent();
textView= (TextView) findViewById(R.id.textView);
textView.setText(intent.getStringExtra(Config.INTENT_TO));
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Second_Activity.class);
startActivityForResult(intent,12);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
String s=data.getStringExtra("sss");
textView.setText(s);
}
}
@Override
protected void onStart() {
super.onStart();
Log.d("myApplication", "onStart ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("myApplication", "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.d("myApplication", "onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("myApplication", "onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.d("myApplication", "onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("myApplication", "onDestroy ");
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啓動第二個界面"
android:id="@+id/button"
android:layout_margin="@dimen/activity_horizontal_margin"
/>
</RelativeLayout>
![這裏寫圖片描述](http:
public class Second_Activity extends Activity{
private EditText editText;
private Button button;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);
editText= (EditText) findViewById(R.id.editText);
button= (Button) findViewById(R.id.button_back);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("sss",editText.getText().toString());
setResult(RESULT_OK,intent);
Second_Activity.this.finish();
}
});
Log.d("second_activity", "onCreate ");
}
@Override
protected void onStart() {
super.onStart();
Log.d("second_activity", "onStart ");
}
@Override
protected void onResume() {
super.onResume();
Log.d("second_activity", "onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("second_activity", "onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.d("second_activity", "onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("second_activity", "onDestroy ");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView android:text="第二個界面"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="返回結果"
android:id="@+id/button_back"/>
</LinearLayout>
![這裏寫圖片描述](http:
全屏、非全屏,橫屏、豎屏
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
intent=getIntent();
textView= (TextView) findViewById(R.id.textView);
textView.setText(intent.getStringExtra(Config.INTENT_TO));
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Second_Activity.class);
startActivityForResult(intent,12);
}
});
}
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="com.example.administrator.myappplication.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
![全屏](http:
![非全屏](http:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="com.example.administrator.myappplication.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
![橫屏](http:
![豎屏](http:
FrameLayout的屬性
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#ff0000"
android:visibility="visible"
android:visibility="invisible"
android:visibility="gone"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp" />
</FrameLayout>
![visible](http:
![invisible](http:
![gone](http:
TableLayout的屬性
/**
android:stretchColumns 設置可伸展的列,自行沾滿剩餘空間
android:shrinkColumns 設置可收縮的列,自行調整大小
android:collapseColumns 設置要隱藏的列。
**/
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2"
android:shrinkColumns="1">
<TableRow>
<Button android:text="button1"/>
<Button android:text="button2"/>
<Button android:text="button3"/>
</TableRow>
<TableRow>
<Button android:text="button1"/>
<Button android:text="button2222222222222222222"/>
<Button android:text="button3"/>
</TableRow>
<TableRow>
<Button android:text="button1"/>
<Button android:text="button2"/>
<Button android:text="button3"/>
</TableRow>
</TableLayout>
![這裏寫圖片描述](http:
Intent
public class ActionActivity extends Activity{
private Button mButtonDIAL;
private Button mButtonCALL;
private Button mBtnWEB;
private Button mBtnSMS;
private Button mBtnDIAO;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action);
mButtonDIAL= (Button) findViewById(R.id.button_DIAL);
mButtonDIAL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:18093193992"));
startActivity(intent);
}
});
mButtonCALL= (Button) findViewById(R.id.button_CALL);
mButtonCALL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:18093193992"));
startActivity(intent);
}
});
mBtnSMS= (Button) findViewById(R.id.button_SMS);
mBtnSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto;"));
intent.putExtra("sms_body","The SMS Text");
startActivity(intent);
}
});
mBtnWEB= (Button) findViewById(R.id.button_WEB);
mBtnWEB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
mBtnDIAO= (Button) findViewById(R.id.button_diao);
editText= (EditText) findViewById(R.id.edittext);
mBtnDIAO.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.administrator.myappplication.MainActivity");
String value=editText.getText().toString();
intent.putExtra(Config.INTENT_TO,value);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edittext"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打電話"
android:id="@+id/button_DIAL" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="直接打電話"
android:id="@+id/button_CALL" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發短信"
android:id="@+id/button_SMS"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打開網頁"
android:id="@+id/button_WEB"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隱式啓動"
android:id="@+id/button_diao"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication" >
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.example.administrator.myappplication.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".Second_Activity">
</activity>
<activity android:name=".ActionActivity">
</activity>
</application>
</manifest>
![這裏寫圖片描述](http:
Extra傳遞數據
public class Second_Activity extends Activity{
private EditText editText;
private Button button;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);
editText= (EditText) findViewById(R.id.editText);
button= (Button) findViewById(R.id.button_back);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("sss",editText.getText().toString());
setResult(RESULT_OK,intent);
Second_Activity.this.finish();
}
});
Log.d("second_activity", "onCreate ");
}
@Override
protected void onStart() {
super.onStart();
Log.d("second_activity", "onStart ");
}
@Override
protected void onResume() {
super.onResume();
Log.d("second_activity", "onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("second_activity", "onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.d("second_activity", "onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("second_activity", "onDestroy ");
}
}
public class MainActivity extends Activity {
private TextView textView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
intent=getIntent();
textView= (TextView) findViewById(R.id.textView);
textView.setText(intent.getStringExtra(Config.INTENT_TO));
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Second_Activity.class);
startActivityForResult(intent,12);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
String s=data.getStringExtra("sss");
textView.setText(s);
}
}
@Override
protected void onStart() {
super.onStart();
Log.d("myApplication", "onStart ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("myApplication", "onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.d("myApplication", "onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("myApplication", "onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.d("myApplication", "onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("myApplication", "onDestroy ");
}
}
public class Config {
public static final String INTENT_TO="qwe123asd";
}