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";
}