Android 布局控件——滾動條視圖,日期,時間


今天學長講了一些控件,比較強的那種控件。

  剛開始講了圖片,但是圖片我前面寫過了就跳過。

滾動條牛牛們應該很熟悉,也常用哈。

  

 

 

 這是垂直的滾動條視圖哈

一起來用吧!

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:fillViewport="true">
<TextView
android:id="@+id/text"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="top"
android:layout_marginBottom="100dp"
android:text="text">
</TextView>

</ScrollView>

主活動:
final TextView t=findViewById(R.id.text);

for (int i = 0; i <30; i++) {
t.append("\n分"+i);
}
效果:

 

 

 注意右邊的那個灰色的,就是滾動條啦!

注意:ScrollView的子元素只能有一個,可以是一個View(如ImageViewTextView等) 也可以是一個ViewGroup
(如LinearLayoutRelativeLayout等),其子元素內部則不再限制,否則會報異常。

本牛崽就犯了這類錯誤,想着嵌套垂直和水平滾動條來着,誰知道搞半天沒用,因為我是直接嵌套進去的,
應該在寫了垂直滾動后添加一種布局,然后在布局里水平滾動就好。

這邊是完全嵌套的滾動條布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">

<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher" />

<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher" />

<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher" />

<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher" />

</LinearLayout>
</HorizontalScrollView>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="內容一"
android:textColor="#03A9F4"
android:textSize="24sp" />

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="內容二"
android:textColor="#03A9F4"
android:textSize="24sp" />

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="內容三"
android:textColor="#03A9F4"
android:textSize="24sp" />

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:layout_marginBottom="80dp"
android:gravity="center"
android:text="內容四"
android:textColor="#03A9F4"
android:textSize="24sp" />
</LinearLayout>
</ScrollView>

 

 

 然后機器人可以向右邊拖

內容可以向下拖。

 

接下來的是日期,就把圖貼上了,注意操作在主活動。

 

 

 

 

主活動主要是獲得當前的日期。並且監聽。

那我就再做個按鈕輸出當前日期到running中。

 

主活動代碼:

 

final DatePicker datePicker = findViewById(R.id.date);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int year = datePicker.getYear();
int month = datePicker.getMonth();
int day = datePicker.getDayOfMonth();
System.out.println("year:" + year + ",month:" + (month + 1) + ",day:" + day);
}
});

 

應該不難理解,上面的剛開始就是找到布局中的日歷,

然后直接給按鈕創建監聽器。按鈕我當時就沒在布局寫了,因為截圖截早了。

 然后就是把他們的年月日找出來唄,這里注意月份0-11,所以month要+1


// 設置選擇日期時的監聽
datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
System.out.println("year:" + year + ",month:" + (monthOfYear + 1) + ",day:" + dayOfMonth);
}
});

監聽事件就這樣唄,這是日期專屬的監聽事件方法,重寫下就看得出和一般監聽器大同小異。

監聽事件的方法應該不用多說吧,日期對象,年,當前年份的月,當前月份的天。

我們就是在日歷上點哪個,就輸出當前的信息(年月日)

 

還有一種比較熱門的日期

<CalendarView
android:id="@+id/calenderView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:firstDayOfWeek="3"
android:shownWeekCount="4"
android:selectedWeekBackgroundColor="#aff"
android:focusedMonthDateColor="#f00"
android:weekSeparatorLineColor="#ff0"
android:unfocusedMonthDateColor="#f9f">
</CalendarView>

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:text="獲取當前日期" />
這個日期它比較特
final CalendarView calendarView=findViewById(R.id.calenderView);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long y=calendarView.getDate();
System.out.println(y);
}
});
殊吧,比如他不能獲取當前的年月日,但是能獲取到1970.1.1的0點到當前時間的毫秒

I/System.out: 1589554516099

這是按鈕監聽器拿到的,

因為長整型范圍也有限,大概只能算到2038年。

然后他也可以使用監聽事件,和上面那個監聽事件一樣。

final CalendarView calendarView=findViewById(R.id.calenderView);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

long date=calendarView.getDate();
String s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(s);
}
});
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {

System.out.println(year+" "+(month+1)+"  "+dayOfMonth);
}
});
改了下,並搭上了它的監聽事件,畢竟都是日期,前面的監聽類型應該是一樣,后面的內容就不一樣了。
剛剛那個長整型的毫秒,用那段話就能轉換成時間,有點奇怪的是時不對,我能想到的是時區的問題,
因為我們是東時區,我們的起始時間其實是1970.1.1的早上八點,
而計算出來的就比當前時間少了八個小時,所以現在系統統計的是外國人的時間,哈哈。

 

都講了日期了,時間是肯定不能少的了,所以把惹人愛的時鍾搬出來了。

不懶了,這我還是寫個按鈕吧!

 

 

 這種不是數字鍾表哈,文化人才看的懂滴。

這邊沒有24小時制,只有上下午夜;但如果是獲取數據就可以24小時制。

日期有他專屬的監聽事件,時間也有;

final TimePicker picker = findViewById(R.id.time);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//二話不說,先搞個按鈕監聽下,把時分都安排上。

int hour = picker.getHour();
int minute = picker.getMinute();//獲取分鍾
System.out.println(hour + ":" + minute);//輸出當前時分,挺遺憾沒有秒這個單位
}
});
// 添加時間選擇的監聽事件
picker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {//和日期的都差不多,
System.out.println(hourOfDay + ":" + minute);//這邊監聽也是獲得當前的時分咯!
}
});

 

 

 

 可以看出監聽的效果了吧,可以比較准確獲得當前系統時間。

 

 

 

今天的內容就這些,又冒出來幾個監聽事件,感覺監聽事件好像很多啊,所以我去搜了下,

監聽事件有這么多,我蒙了

https://blog.csdn.net/weixin_41101173/article/details/81270133

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM