學(xué)習(xí)內(nèi)容
- 面向?qū)ο笈c面向過程的不同
- 實戰(zhàn)演練———抽獎APP
1.面向?qū)ο笈c面向過程的不同
(1)Java與C語言不同之處就在于Java是面向?qū)ο蟮模鳦語言是面向過程的
(2)面向過程
面向過程就是分析實現(xiàn)需求的步驟掘宪,通過函數(shù)一步一步實現(xiàn)這些步驟,接著依次調(diào)用這些函數(shù)刀疙。用創(chuàng)建登錄界面為例
登錄界面.png
a.寫一個文本框,顯示“姓名”
b.寫一個輸入框
c.寫一個文本框扫倡,顯示“密碼”
d.寫一個輸入框
e.寫一個按鈕
f.寫一個功能谦秧,接收用戶輸入的信息
g.寫一個功能竟纳,接收用戶輸入的密碼
h.寫一個功能,接收用戶點(diǎn)擊事件
(3)面向?qū)ο?br> 把整個需求按照特點(diǎn)疚鲤、功能化分锥累,將這些存在共性的部分封裝成對象,創(chuàng)建對象不是為了完成某一步驟集歇,而是為描述某個事物在解決問題的步驟中的行為桶略。
登錄界面1.png
2.實戰(zhàn)演練———抽獎APP
代碼
xml配置文件使用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<!-- 文本框 -->
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#2F2E2F"
android:text="你懂得"
android:textColor="#FFF"
android:textSize="30sp"
android:gravity="center"
/>
<!-- 按鈕-->
<Button
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="#D65489"
android:layout_marginTop="400dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:text="開始抽獎"
android:textColor="#FFF"
android:textSize="20sp"
android:onClick="start"/>
</RelativeLayout>
MainActivity代碼塊
package com.example.luckyman;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
//創(chuàng)建數(shù)組,保存候選人
String [] names = new String[]{"上官婉兒","后裔","安其拉","諸葛亮","武則天","曹操","劉邦","劉備"};
//創(chuàng)建一個定時器
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view){
//將View轉(zhuǎn)化為Button
Button btn=(Button)view;
//獲得當(dāng)前的的標(biāo)題
String title = btn.getText().toString();
//判斷按鈕的標(biāo)題
if(title.equals("開始抽獎")){
//設(shè)置為暫停
btn.setText("暫停");
//創(chuàng)建定時器
timer=new Timer();
//每隔一段時間去執(zhí)行一個任務(wù)
timer.schedule(new TimerTask() {
@Override
public void run() {
produceOnePeople();
}
},0 ,100);
}else{
//設(shè)置為抽獎
btn.setText("開始抽獎");
//關(guān)閉定時器
timer.cancel();
}
produceOnePeople();
}
//產(chǎn)生一個隨機(jī)的人名诲宇,顯示到文本框上
public void produceOnePeople(){
Random random=new Random();
int index = Math.abs(random.nextInt())%names.length;
//從數(shù)組中取出名字
String name=names[index];
//將名字顯示到文本框上
TextView tv = findViewById(R.id.tv_name);
tv.setText(name);
}
}
package com.example.luckyman;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
//創(chuàng)建數(shù)組际歼,保存候選人
String [] names = new String[]{"上官婉兒","后裔","安其拉","諸葛亮","武則天","曹操","劉邦","劉備"};
//創(chuàng)建一個定時器
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view){
//將View轉(zhuǎn)化為Button
Button btn=(Button)view;
//獲得當(dāng)前的的標(biāo)題
String title = btn.getText().toString();
//判斷按鈕的標(biāo)題
if(title.equals("開始抽獎")){
//設(shè)置為暫停
btn.setText("暫停");
//創(chuàng)建定時器
timer=new Timer();
//每隔一段時間去執(zhí)行一個任務(wù)
timer.schedule(new TimerTask() {
@Override
public void run() {
produceOnePeople();
}
},0 ,100);
}else{
//設(shè)置為抽獎
btn.setText("開始抽獎");
//關(guān)閉定時器
timer.cancel();
}
produceOnePeople();
}
//產(chǎn)生一個隨機(jī)的人名,顯示到文本框上
public void produceOnePeople(){
Random random=new Random();
int index = Math.abs(random.nextInt())%names.length;
//從數(shù)組中取出名字
String name=names[index];
//將名字顯示到文本框上
TextView tv = findViewById(R.id.tv_name);
tv.setText(name);
}
}