安卓基础(一)

按钮点击事件

  • 通过内部类来实现
package com.example.administrator.helloworld;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class helloworld extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helloworld);
        if (checkSelfPermission(Manifest.permission.CALL_PHONE) == PERMISSION_DENIED) {
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0x11);
        }
        ((Button) findViewById(R.id.button2)).setOnClickListener(new buttonClickListener());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 0x11:
                break;
            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private class buttonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            EditText textView = findViewById(R.id.editText2);
            String strNumber = textView.getText().toString().trim();
            if (strNumber.isEmpty()) {
                Toast.makeText(helloworld.this, "电话号码不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            if (checkSelfPermission(Manifest.permission.CALL_PHONE) == PERMISSION_DENIED) {
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0x11);
                return;
            }
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + strNumber));
            startActivity(intent);

        }
    }
}
  • 匿名内部类
 ((Button) findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
  • 在当前类直接实现该接口
public class helloworld extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helloworld);
        if (checkSelfPermission(Manifest.permission.CALL_PHONE) == PERMISSION_DENIED) {
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0x11);
        }
        ((Button) findViewById(R.id.button2)).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                break;
        }
    }
  • 自定义事件
    如果要自定义事件,可以通过button的属性指定接收的方法,然后在类里面定义该方法。
    声明属性:
 <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="拨打"
        android:onClick="selfDefine"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

在代码中添加该方法

public class helloworld extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_helloworld);
        if (checkSelfPermission(Manifest.permission.CALL_PHONE) == PERMISSION_DENIED) {
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 0x11);
        }
        ((Button) findViewById(R.id.button2)).setOnClickListener(this);
    }
    public void selfDefine(View v) {

    }

android中常用的布局

  • 线性布局
    新建一个线性布局layout
    这里写图片描述
    给布局添加方向
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>

给item添加margin

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="请输入电话号码"/>
</LinearLayout>
  • 相对布局

效果图
这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入电话号码0"/>
    <TextView
        android:id="@+id/tv_number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入电话号码1"
        android:layout_below="@id/tv_number"/>
    <TextView
        android:id="@+id/tv_number2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入电话号码2"
        android:layout_below="@id/tv_number1"/>
    <TextView
        android:id="@+id/tv_number3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入电话号码3"
        android:layout_toRightOf="@id/tv_number2"
        android:layout_below="@id/tv_number1"/>
</RelativeLayout>
  • 帧布局
    帧布局的意思是上面的控件是一层一层显示的
    在edit中间放一个button
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈"
        android:layout_gravity="center"/>

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="请输入电话号码" />
</FrameLayout>
  • 表格布局
    效果图
    这里写图片描述
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--代表一行-->
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="哈哈哈"
            android:textColor="#ff0000"
            android:textSize="18sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="hehe"
            android:textColor="#00ff00"
            android:textSize="18sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="ssssss"
            android:textColor="#ffff00"
            android:textSize="18sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="aaaaaa"
            android:textColor="#100ff0"
            android:textSize="18sp"/>
    </TableRow>
</TableLayout>

猜你喜欢

转载自blog.csdn.net/kebiaoy/article/details/81039006
今日推荐