[Android学习]布局相关

【摘要】可能小东急的笔记比较emmm~简陋,但是我能看懂相信你们那么聪明也能看懂~UIoverview:http://androiddoc.qiniudn.com/guide/topics/ui/overview.html1.View&viewgroup:&...

可能小东急的笔记比较emmm~简陋,但是我能看懂相信你们那么聪明也能看懂~

UIoverview:http://androiddoc.qiniudn.com/guide/topics/ui/overview.html

1.View&viewgroup:

    在Android APP中,所有的用户界面元素都是由View和ViewGroup的对象构成的。View是绘制在屏幕上的用户能与之交互的一个对象。而ViewGroup则是一个用于存放其他View(和ViewGroup)对象的布局容器!


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="I am a Button" />
</LinearLayout>


2.LinearLayout(线性布局):

主线程当中不允许子线程更新主线程的UI

主线程是用来相应用户操作的

可以通过handler

下面写了第一个APP

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.sassassaisn.firstapp.MainActivity">


    <EditText
        android:id="@+id/inputtime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="输入时间" />

    <Button
        android:id="@+id/gettime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取倒计时时间" />

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/starttime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始计时" />

    <Button
        android:id="@+id/stoptime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止计时" />
</LinearLayout>

MainActivity.java:

package com.example.sassassaisn.firstapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;



public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText inputTime;
    private Button getTime,startTime,stopTime;
    private TextView Time;
    private int ii = 0;
    private Timer timer = null;
    private TimerTask task = null;

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

    private void initView(){
        inputTime = (EditText) findViewById(R.id.inputtime);
        getTime = (Button) findViewById(R.id.gettime);
        startTime = (Button) findViewById(R.id.starttime);
        stopTime = (Button) findViewById(R.id.stoptime);
        Time = (TextView) findViewById(R.id.time);
        getTime.setOnClickListener(this);
        startTime.setOnClickListener(this);
        stopTime.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.gettime:
                Time.setText(inputTime.getText().toString());
                ii = Integer.parseInt(inputTime.getText().toString());
                break;
            case R.id.starttime:
                startTime();
                break;
            case R.id.stoptime:
                stopTime();
                break;
            default:
                break;
        }
    }

    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg) {
            Time.setText(msg.arg1 + "");
            startTime();
        }
    };

//    开始计时
    public void startTime(){
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                ii--;
                Message message = mHandler.obtainMessage();
                message.arg1 = ii;
                mHandler.sendMessage(message);
            }
        };
        timer.schedule(task,1000);//执行对象+毫秒为单位

    }
//  停止计时
    public void stopTime(){
        timer.cancel();//清除
    }


}


未经允许不得转载:第一资源网 » [Android学习]布局相关

 小东
 简介:专业团队网站开发、安全运维,合作意向请联系!

扫码关注微信公众号:ITDYBOY,学前端,学安全,从0到1,从1到精通!

扫码关注微信公众号:ITDYBOY

发表评论

游客
送你一朵小花花~

帅人已评(14)

鼓掌https://blog.csdn.net/nghuyong/article/details/53738289
LV 1 爱你 6年前 (2018-04-10) 回复