Android:日常学习笔记(4)——探究活动(1)

Android:日常学习笔记(4)——探究活动

什么是活动:

  活动是最容易吸引用户的地方,它是一种可以包含用户界面的组件,主要用于和用户进行交互。

手动创建活动

创建空活动

1.新建活动时选择Add No Activity

2.新建EmptyActivity

3.点击Finish完成

创建和加载布局

说明:

  前面我们说过,Android程序的设计讲究逻辑与视图分离,最好每一个活动都能对应一个布局。
  布局就是用来显示界面内容的,因此我们现在就手动创建一个布局文件。

1.在res目录下新建一个layout目录

2.右键新建一个Layout resource file

3.完成后展开布局管理器


4.添加按钮查看效果

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />
    <!--@+id/button:表示按钮的唯一标识符-->
    <!--match_parent:表示和父元素一样宽-->
    <!--wrap_content:只要能刚好显示内容即可-->

5.加载布局文件

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
    }
}
6.在AndroidManifest中注册

所有活动都要在AndroidManifest中注册才能生效.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zy.android1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity"></activity>
    </application>

</manifest>

7.运行查看效果

在活动中使用Toast

什么是Toast

  Toast是一种Android系统提供的非常好的提醒方式,在程序中可以使用它将一些短小的信息通知给用户,这些消息会在一段时间后自动消失,并且不占用任何屏幕空间.

让按钮弹出Toast

代码:
public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button = (Button) findViewById(R.id.button);
        //在活动中可以通过findViewById来获取布局文件中定义的元素
        button.setOnClickListener(new View.OnClickListener() {
        //为button注册一个监听器
            public void onClick(View v) {
                Toast.makeText(FirstActivity.this,"You click the button",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
说明:
1.public static Toast makeText(Context context, CharSequence text, int duration) 制作一个仅仅包含文本信息的标准Toast
2.最后一个参数为Toast显示时间长短
字段摘要 
static int LENGTH_LONG            Show the view or text notification for a long period of time.
static int LENGTH_SHORT            Show the view or text notification for a short period of time.


效果:
  

在活动中使用Menu

说明

  手机屏幕毕竟有限,为了在活动中显示菜单,Android给我们提供了一种方式,可以 让菜单都能得到展示的同时,还不占用任何屏幕。

实现

新建菜单配置文件

输入菜单配置项
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/add_item" android:title="Add"/>
        <item android:id="@+id/remove_item" android:title="Remove"/>
</menu>
重写onCreateOptionsMenu方法和onOptionsItemSelected方法
    public boolean onCreateOptionsMenu(Menu menu) {
        //MenuInflater:This class is used to instantiate menu XML files into Menu objects.
        /*  menuRes - Resource ID for an XML layout resource to load (e.g., R.menu.main_activity)
            menu - The Menu to inflate into. The items and submenus will be added to this Menu.*/
        getMenuInflater().inflate(R.menu.main,menu);
        return true;//允许菜单显示出来
    }
        public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.add_item:
                Toast.makeText(this,"You click Add",Toast.LENGTH_SHORT).show();break;
            case R.id.remove_item:
                Toast.makeText(this,"You click Remove",Toast.LENGTH_SHORT).show();break;
                default:break;
        }
        return true;
    }

销毁一个活动

只要按一下Back键就会销毁当前活动。也可以使用finsh命令,来完成销毁。
        button.setOnClickListener(new View.OnClickListener() {
        //为button注册一个监听器
            public void onClick(View v) {
                Toast.makeText(FirstActivity.this,"You click the button",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
原文地址:https://www.cnblogs.com/MrSaver/p/6555111.html