一、三种显示意图
(一)实现代码
三种方式中的前两种都是打开当前activity的界面,第三种是打开另一个APP的activity
- 新建两个activity,命名为DateActivity和ClockActivity,工程目录结构如下
- 选择activity_main.xml文件,添加三个button控件
时钟button控件
<Button
android:id="@+id/Clock_Btn"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_x="69dp"
android:layout_y="152dp"
android:text="方式一"
android:textSize="35sp"
tools:layout_editor_absoluteX="57dp"
tools:layout_editor_absoluteY="145dp" />
日历button控件
<Button
android:id="@+id/Calendar_Btn"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_x="71dp"
android:layout_y="281dp"
android:text="方式二"
android:textSize="35sp"
tools:layout_editor_absoluteX="57dp"
tools:layout_editor_absoluteY="317dp" />
跳转其它APP的控件
<Button
android:id="@+id/actother_Btn"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_x="71dp"
android:layout_y="412dp"
android:text="方式三"
android:textSize="35sp"
tools:layout_editor_absoluteX="57dp"
tools:layout_editor_absoluteY="502dp" />
采用绝对布局
- 设计布局
- 添加时间控件
- 设计布局
- 添加日期控件
- 设计布局
- 主界面的编写
实现OnClick接口
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
先定义按钮
Button type1_btn;
Button type2_btn;
Button type3_btn;
实例化三个按钮
type1_btn= (Button) findViewById(R.id.Clock_Btn);
type2_btn= (Button) findViewById(R.id.Calendar_Btn);
type3_btn= (Button) findViewById(R.id.actother_Btn);
监听器
type1_btn.setOnClickListener(this);
type2_btn.setOnClickListener(this);
type3_btn.setOnClickListener(this);
点击方法
public void onClick(View v) {
Intent intent=new Intent();
switch (v.getId()){
case R.id.Clock_Btn: //方式一
intent.setClass(this,ClockActivity.class);
break;
case R.id.Calendar_Btn: //方式二
intent.setClassName(this,"com.example.app2.DateActivity"); //要打开的全类名
break;
case R.id.actother_Btn: //方式三
intent.setClassName("com.example.hello","com.example.hello.MainActivity"); //要打开的项目名,要打开项目的全类名
break;
}
startActivity(intent);
}
(二)实现效果
二、隐式意图
一个没有声明intent-Filter的组件只能响应指明自己名字的显示意图请求,而无法响应隐式意图请求,而一个声明了intent-Filter的组件既可以相应显示意图请求,也可以响应隐式意图请求。
Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类别)找到最合适的组件来处理这个意图