UI设计探索之初级入门 | 青训营笔记

87 阅读2分钟

这是我参与「第四届青训营」笔记创作活动的第3天。

记录一些基本控件的使用方法
1、重写创建菜单 直接在对应的activity.java文件中写\

@Override
public boolean onCreateOptionsMenu(Menu 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:
    }
    return true;
}

2、onCreate()函数的重写 Bundle经常使用在Activity之间或者线程间传递数据, 传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组, 也可以是对象或对象数组。
基本框架如下所示:
调用setContentView()方法,给当前活动加载布局,传入布局文件的id R.layout.first_layout

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.first_layout);
     }

接下来加入按钮\

Button button1=(Button)findViewById(R.id.button_1);
Button button2=(Button)findViewById(R.id.button_2);

通过findViewById()方法获取到在布局文件的元素,传入id,得到按钮的实例。
findViewById()方法返回view对象,需要向下转型成Button对象

设置按钮被点击后的事件
匿名内部类 为按钮注册监听器setOnClickListener(),点击按钮时执行OnClick()方法
Toast:消息提醒方式,一段时间自动消失
用法:静态方法makeText创建Toast对象,调用show显示出来 makeText()三个参数:1、Context 2、Toast显示的文本 3、Toast的显示时长\

Intent是Android程序中各组件进行交互的一种重要方式,用于启用活动、启用服务、发送广播等场景
分为显式Intent和隐式Intent
构造函数: Intent(Context packageContext,Class)
第一个参数Context要求提供启动活动的上下文,第二个指明目标活动 用startActivity(intent)启动目标活动

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      Toast.makeText(FirstActivity.this, "you clicked Button1", Toast.LENGTH_SHORT).show();
        //Intent intent2=new Intent("com.example.activitytest.ACTION_START");
       //intent2.addCategory("com.example.activitytest.MY_CATEGORY");
        //startActivity(intent2);
      //  Intent intent4=new Intent(Intent.ACTION_VIEW);
       // intent4.setData(Uri.parse("http://www.baidu.com"));
        //startActivity(intent4);
        Intent intent5=new Intent(Intent.ACTION_DIAL);
        intent5.setData(Uri.parse("tel:18669688017"));
        startActivity(intent5);
        //隐式intent,给出更抽象的action和category信息,交由系统分析这个intent
        //<activity>标签下配置<intent-filter>的内容
    }
});

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(FirstActivity.this, "you clicked Button2", Toast.LENGTH_SHORT).show();
        String data="~~~~~~~hello wuluketao!!!!!!!!!!!~~~~~~";
        Intent intent1 =new Intent(FirstActivity.this,SecondActivity.class);
        intent1.putExtra("extra_data",data);
        startActivity(intent1);
        //finish();
        //显式intent启动活动时传递数据,通过putExtra()方法传递参数
    }
});

3、xml布局文件的设计

<?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">
    <com.example.activitytest.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"
        />
    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hi,这里是无露可逃!你升沧了吗?"
        android:gravity="center"
        android:textSize="24sp"
        android:textColor="#00ff00"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

综合效果如下:
a56e50d2b430a49ace55f20f9208d6c.png