Activity的初步应用
我们能够看到的,并且能够与之交互的页面,都是通过Activity来实现的
定义: Activity是提供全屏界面,实现与用户交互的应用程序组件。
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
EditText nameEt = (EditText) findViewById(R.id.name_id);
}
Button btn=(Button)findViewById(R.id.button);btn.setOnClickListener(new View.OnClickListener()
{ @Override public void onClick(View v) { Toast toast = Toast.makeText(MainActivity.this, "Activity对你的操作响应了!",Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 240);toast.show(); }});
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bw.fancx.bzh_day05">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
>
<activity android:name = ".MainActivity“ >
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
</application>
</manifest>
<activity
android:name=".InfoActivity"
android:label="内容页面"
android:screenOrientation="landscape"
/>
定义: Context是一个用来为应用程序提供环境信息的全局对象。
什么是Intent
定义: Intent是用于各组件或应用之间通讯时携带信息的对象。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<LinearLayout
... ...
>
<ImageView
android:id="@+id/iv_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/slide3"
/>
</LinearLayout>
public class InfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/slide2"
>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImageView = (ImageView) findViewById(R.id.iv_id);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this , InfoActivity.class);
startActivity(intent);
}
});
}
}
String TAG="PrintOut";
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG,"onCreate");
}
@Overrideprotected void onStart() { super.onStart(); Log.i(TAG,"onStart");}
@Overrideprotected void onPause() { super.onPause(); Log.i(TAG,"onPause");}
@Overrideprotected void onStop() { super.onStop(); Log.i(TAG,"onStop");}
@Overrideprotected void onRestart() { super.onRestart(); Log.i(TAG,"onRestart");}
@Overrideprotected void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy");}
如何设置启动模式
<activity
android:name=".TeaActivity“ android:launchMode=" standard "></activity>
<activity
android:name=".InfoActivity“ android:launchMode=" standard " ></activity>
<activity
android:name=".InfoActivity“ android:launchMode=" singleTop" ></activity>
<activity
android:name=".InfoActivity“ android:launchMode=" singleTask" ></activity>
<activity
android:name=".InfoActivity“ android:launchMode=" singleInstance" ></activity>
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {
Intent intent=new Intent(MainActivity.this,TeaActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});