0
点赞
收藏
分享

微信扫一扫

Android入门(一)

曾宝月 2022-04-21 阅读 70

安装略…

1.android 常用单位

  • px=像素
  • dp=虚拟像素,在不同的像素密度的设备上会自动适配,推荐使用
  • sp=同dp相似,主要用来显示文字,还会根据用户的字体大小偏好来缩放

2.match_parent、fill_parent、wrap_content区别

3.相对布局子控件属性

4.页面跳转+页面传参+接收到参数后显示到页面

主页

package com.example.myapplicationdemo1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn= findViewById(R.id.lineLaout);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                跳转方式一
//                Intent i = new Intent(当前类,跳转到的指定类);
//                Intent i = new Intent(MainActivity.this , test.class);
//                跳转方式二
                Intent i=new Intent();
//                intent.setClass(当前类,跳转到的指定类);
                i.setClass(MainActivity.this,test.class);
                //携带参数跳转(2种)
                //----------1
                i.putExtra("name","张三");
                //----------2
                Bundle bundle=new Bundle();
                bundle.putString("name2","王五");
                bundle.putInt("id",12);
                i.putExtras(bundle);
                startActivity(i);
            }
        });
    }
    public void select(View view){
        Toast.makeText(this,"按钮被点击啦",Toast.LENGTH_LONG).show();
    }
}

跳转页test

package com.example.myapplicationdemo1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class test extends AppCompatActivity {
    private String data;
    private Integer id;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Intent i=this.getIntent();
        Bundle bundle=this.getIntent().getExtras();
        data=i.getStringExtra("name");
        name=bundle.getString("name2");
        id=bundle.getInt("id");
        TextView textView=findViewById(R.id.text1);
        textView.setText("我的名字叫:"+data+",id是:"+id);//更新到页面
        System.out.println("===================name>"+data);
        System.out.println("===================name>"+name+","+id);

    }
}
举报

相关推荐

0 条评论