安卓开发



title: 安卓开发
date: 2020-11-13 10:31:26
tags: 安卓开发


  1. android studio 4.0 Device Monitor 在哪里?

[参考博客][https://blog.csdn.net/qq_42023080/article/details/105842271]:"%SDK路径%\tools\lib\monitor-x86_64\monitor.exe"

问题

快捷键

https://docs.oracle.com/javase/tutorial/tutorialLearningPaths.html

https://developer.android.google.cn/studio/intro

沙盒是在受限的安全环境中运行应用程序的一种做法,这种做法是要限制授予应用程序的代码访问权限。

1.1 显示端口被占用

image-20201102221314188

1.1.1 如何查看windows 端口占用情况

window+R组合键
`netstat -ano`,列出所有端口的情况
查看被占用端口对应的PID,输入命令:netstat -aon|findstr "49157",回车,记下最后一位数字,即PID,这里是2720
继续输入tasklist|findstr "2720",回车,查看是哪个进程或者程序占用了2720端口

image-20201102223355629

image-20201102224058279


一个APP中有多个Acitivity,默认首页是哪个

image-20201102224422414

权限的概念:比如要在APP中显示一张网上的图片需要在AndroidManifest文档中加入

03、单击事件

image-20201103093634932

输入错误的提示

限制密码个数

image-20201103094326801

  1. 程序中实现事件监听器,通常有以下四种形式

    • Activity本身作为事件监听器:通过Activity实现监听器接口,并实现事件处理方法
    • 匿名内部类形式:使用匿名内部类创建事件监听器对象,内部类或外部类形式,将事件监听类定义为当前的内部类或普通外部类
    • 绑定标签:在布局文件xml中为指定标签绑定事件处理方法
    使用onClick属性
    使用OnClickListener实例
    1重写onClick()方法
    

    选中变量;CTRL ALT + F,从方法中创建全局变量

04、登录页面的单击事件

如果获取前端组件的信息,并在后端做出业务处理,反馈到前端

05、按钮设计+页面跳转 页面跳转Demo_1

image-20201103115149398

image-20201103115233222

image-20201103115256608

[在Android Studio的drawable文件下创建XML文件][https://blog.csdn.net/liqian321/article/details/48519349]

页面跳转Demo_1

image-20201103135937107

? [类的访问控制,public,private,protect][https://baijiahao.baidu.com/s?id=1630621164727182416&wfr=spider&for=pc]

06、相对布局

image-20201103143549985

image-20201103143633852

image-20201103143720628

image-20201103144532278

07、相对布局和线性布局结合的实例 Demo_03

08、欢迎界面的实现

3s后自动跳转

09、单向数据传递

// 传递
Intent intent = new Intent();
intent.putExtra("data",str);//设置键值对
startActivity(intent);//激活意图
// 获取
Intent intent = new Intent();//获取意图
String str = Intent.getStringExtra("data");//获取传递的值

10、双向数据传递 Demo_06

image-20201104071031695

如果想在Activity中得到新打开Activity关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent,int requestCode)方法`打开新的Activity,新Activity关闭前需要向前面的Activity返回数据需要使用系统提供的setResult(int resultCode,Intent data)方法实现;为了得到返回的数据,必须在前面的Activity中重写onActivityResult(int requestCode,int resultCode,Intent data) 方法

//MainActivity.class
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
                intent.putExtra("001",eT1.getText().toString());
                startActivityForResult(intent,0x01);
            }
        });//发送请求
//接收来自MainActivity2.class数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String stringExtra = data.getStringExtra("002");
        eT2.setText(stringExtra);
    }

        jieshou = (EditText) findViewById(R.id.jieshou);
        jieshou.setText(getIntent().getStringExtra("001"));

        fasong = findViewById(R.id.fasong);

        btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                intent.putExtra("002",fasong.getText().toString());
                setResult(RESULT_OK,intent);
                finish();//只有调用finish()第二个页面才会自动关闭回到第一个界面
            }
        });

11、音乐生日卡Demo_07

需求:点击图片的任意区域都可以开始播放视频

可以给Linerout.setOnclick(new )

12、单选按钮 Deom_08_Activity

  • 如何正确加载单选按钮:

    • 添加RadioGroup,在其中添加RadioButton

    •     btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  for (int i = 0;i< radioGroup.getChildCount();i++){
                      radioButton = (RadioButton) radioGroup.getChildAt(i);
                      if (radioButton.isChecked()){
                          Toast.makeText(getApplicationContext(), "您的选择是"+radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
                      }
                  }
        
              }
          });
      
    • radioGroup.getChildAt() ;radioGroup.getChildCount();radioButton.isChecked()

13、复选框_Demo_

  • CheckBox与普通按钮的不同在于多了一个可选中的的功能,多了一个Android:checked属性,该属性用于RadioButton、CheckBox初始时是否被选中

  •     btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //定义字符串存储喜欢的东西
                love = "";
                CheckBox[] checks = new CheckBox[] {checkBox1,checkBox2,checkBox3};
                for (int i = 0;i

14、ToggleButton、Switch Demo_08_MainActivity2

ToggleButton(开关按钮):具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本

    


        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    imageView.setImageResource(R.drawable.on);
                } else {
                    imageView.setImageResource(R.drawable.off);
                }
            }
        });

switch和ToggleButton相类似:

     
switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    imageView.setImageResource(R.drawable.on);
                } else{
                    imageView.setImageResource(R.drawable.off);
                }
            }
        });

15、简单对话框Demo_08_

使用AlertDialog,创建各种功能和风格的对话框。该类对话框可以只是显示对话框消息,也可以为按钮注册监听器实现相应的功能

btn_activity_3 = (Button) findViewById(R.id.buton);
btn_activity_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity3.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("提示");
                builder.setMessage("这里是");
//                确定和取消的按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"Ok",Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"NO",Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();//注意,AlterDialog需要show()
            }

        });

16、简单列表项对话框

AlterDialog.Builder调用setltems()方法可设置简单列表对话框,需要注意的是调用该方法时需要传入一个数组或数组资源的ID,其中参数which为选择哪一项编号

      btn = (Button) findViewById(R.id.button);       btn.setOnClickListener(new View.OnClickListener() {           @Override           public void onClick(View v) {               AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity1.this);               builder.setIcon(R.drawable.ic_launcher_foreground);               builder.setTitle("啥是Title");               builder.setMessage("简单对话列表");               //关键↓:给builder.setItems()中出入String[]和               builder.setItems(item, new DialogInterface.OnClickListener() {                   @Override                   public void onClick(DialogInterface dialog, int which) {                       Toast.makeText(getApplicationContext(),"您选择的是:"+item[which],Toast.LENGTH_SHORT).show();                   }               });               builder.show();           }       });

18、单选列表对话框Deom_09_Activity2

AlterDialog.Bulider调用setSingleChoiceItems()方法即可创建带单选列表项的对话框

btn_2 = (Button) findViewById(R.id.button_Activity2);btn_2.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);        builder.setTitle("性别");        //设置单选列表,参数1是某人选择item[1]        builder.setSingleChoiceItems(item, 1, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                content=item[which];            }        });              builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(getApplicationContext(),content,Toast.LENGTH_SHORT).show();            }        });        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(getApplicationContext(),"您已取消:"+content,Toast.LENGTH_SHORT).show();            }        });        builder.show();    }});

19、多选列表对话框Demo_10

builder.setMultiChoiceItems(字符数组,布尔数组,监听器)

20、适配器的使用Adapter/Demo_11

Adapter接口

在Android中大量的使用到了ListView,而ListView需要与各种适配器结合才能够使用。ArrayAdapter(数组适配器)一般用于显示一行文本信息,所以比较容易。

  1. 创建ArrayAdapter

    listView = (ListView) findViewById(R.id.listView)
    public ArrayAdapter(Context context,int textViewResourceld,List objects)ArrayAdapter接口

R.layout.item.xml 文件,设置一个模板的布局据

<?xml version="1.0" encoding="utf-8"?>
  1. <?xml version="1.0" encoding="utf-8"?>    
    
public class MainActivity extends AppCompatActivity {    String items[] = new String[] {"大数据","云计算","互联网","大数据","云计算","互联网","大数据","云计算","互联网"};    private ListView listview;    private ArrayAdapter arrayAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listview = (ListView) findViewById(R.id.listview);        arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.item, items);        //R.layout.item.xml定义“单元格”的样式        listview.setAdapter(arrayAdapter);//数据和样式相联系        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Toast.makeText(getApplicationContext(),""+items[position],Toast.LENGTH_SHORT).show();            }        });    }/*    public interface OnItemClickListener {        /**         * Callback method to be invoked when an item in this AdapterView has         * been clicked.         * 

* Implementers can call getItemAtPosition(position) if they need * to access the data associated with the selected item. * * @param parent The AdapterView where the click happened. * @param view The view within the AdapterView that was clicked (this * will be a view provided by the adapter) * @param position The position of the view in the adapter. * @param id The row id of the item that was clicked. void onItemClick(AdapterView<?> parent, View view, int position, long id);}*/}

21、 SimpleAdapter-Adapter接口2 Demo_12

适配器:在安卓中,顾名思义就是把数据变成符合界面风格的形式,并且通过List View显示出来。适配器是数据和界面之间的桥梁

image-20201109113051623

 class MainActivity extends AppCompatActivity {    String items[] = new String[] {"1","2","3","4","5","6","7","8"};    int images[] =new int[]{R.drawable.ie,R.drawable.map,R.drawable.music,R.drawable.wc,R.drawable.ie,R.drawable.map,R.drawable.music,R.drawable.wc};    String advertise[] = new String[] {"月榜第一","日活第一","涨粉第一","下载第一","年度最佳","拉跨不行","年度最佳","拉跨不行"};         private ArrayList> list;    private SimpleAdapter simpleAdapter;	private ListView listview;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //1 抽取列表视图        listview = (ListView) findViewById(R.id.listview);        //2 实现list中具体的数据        list = new ArrayList<>();        for(int i=0;i map = new HashMap<>();            map.put("001",images[i]);            map.put("002",items[i]);            map.put("003",advertise[i]);            list.add(map);//将map键值对装填进入list中        }        //3. 设计布局文件        simpleAdapter = new SimpleAdapter(getApplicationContext(), list, R.layout.bg, new String[]{"001", "002","003"}, new int[]{R.id.imageView, R.id.textView1,R.id.textView2});        //4.设置adapter文件        listview.setAdapter(simpleAdapter);        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Toast.makeText(getApplicationContext(),items[position],Toast.LENGTH_SHORT).show();            }        });    }}
<?xml version="1.0" encoding="utf-8"?>                     

13、spinner高级组件 Demo_13

三个文件需要改动

res/values/strings.xml

    Demo_13            --您选择的学校是--        西安电子科技大学        成都电子科技大学        北京邮电大学    
        spinner = findViewById(R.id.spinner);        //设置spinner的监听器        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {           //如何获取spinner选中的item,AdapterView.getItemAtPosition(int:position)            @Override            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {                String school = parent.getItemAtPosition(position).toString();                textView.setText(school);            }            //↓不需要理会            @Override            public void onNothingSelected(AdapterView<?> parent) {            }        });

23、 gridview网络视图Demo_14

gradView的一些属性

属性 含义
android:numColumns="auto_fit" 列数设置为自动
android:columnWidth="90dp" 每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth" 缩放与列宽大小同步
android:verticalSpacing="10dp" 垂直边距
android:horizontalSpacing="10dp" 水平边距

如何使用SimpleAdapterd

  1. 获取承接数据的gridView或者ListView
  2. 准备数据,利用ArrayList>,HashMap<>
  3. SimpleAdapter类的实例化
  4. 给对应组件装配Adpter
public class MainActivity extends AppCompatActivity {    private GridView gridView;    String items[] = new String[] {"计算器","拍照","浏览器","地图","音乐","设置","微信"};    int images[] = new int[] {R.drawable.calcuat,R.drawable.camera,R.drawable.ie,R.drawable.map,R.drawable.music,R.drawable.seting,R.drawable.wc};    private ArrayList> data;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//1.获取承接数据的gridView或者ListView        gridView = (GridView) findViewById(R.id.gridView);//2.准备数据,利用ArrayList>,HashMap<>        data = new ArrayList>();        for (int i =0;i map = new HashMap<>();            map.put("001",items[i]);            map.put("002",images[i]);            data.add(map);        }//3.SimpleAdapter类的实例化        SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(), data,R.layout.bg,new String[]{"001","002"},new int[]{R.id.textView,R.id.imageView});//4.给对应组件装配Adpter        gridView.setAdapter(simpleAdapter);    }}

14、WebView实现,enter键事件

WebView是安卓提供显示web界面的工具类,可以像PC端的浏览器那样进行显示。

实现方法:view.loadUrl(url);

image-20201110214618665

相关