Android studio入門到精通實體實驗
實驗內容:
----------------- 簡單的考試程式----------------
實驗步驟:
一、打開Android studio,新建一個專案;


二、寫入代碼;

<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android基礎知識測評"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
<EditText
android:id="@+id/et_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:ems="3"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="0.615"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/di1ti"
app:layout_constraintStart_toEndOf="@+id/et_on"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/di2ti"
app:layout_constraintStart_toStartOf="@+id/et_on"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="113dp"
android:layout_height="64dp"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3">
<RadioButton
android:id="@+id/rd1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開源的" />
<RadioButton
android:id="@+id/rd2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="非開源的" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/di3ti"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:text="JAVA"
app:layout_constraintStart_toEndOf="@+id/cb1"
app:layout_constraintTop_toBottomOf="@+id/textView4"
android:layout_marginLeft="60dp" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JDK"
app:layout_constraintStart_toStartOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:text="SDK"
app:layout_constraintStart_toEndOf="@+id/cb2"
app:layout_constraintTop_toBottomOf="@+id/textView4"
android:layout_marginLeft="60dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/di4ti"
app:layout_constraintStart_toStartOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/cb1" />
<Spinner
android:id="@+id/spinner"
android:layout_width="130dp"
android:layout_height="30dp"
android:entries="@array/Systems"
app:layout_constraintStart_toStartOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="104dp"
android:layout_marginBottom="36dp"
android:text="總分:"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="104dp" />
<TextView
android:id="@+id/sorce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F1ED06"
android:text=" 00 "
app:layout_constraintBottom_toBottomOf="@+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toTopOf="@+id/textView6"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/subscribe"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

package com.example.a37;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity {
static int i = 0,cb_num = 0;
boolean rd_num = false,sp_num = false,et_num = false;
TextView sorce;
TextView et_on;
RadioGroup rd;
Button button;
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定義
et_on = findViewById(R.id.et_on);
sorce = findViewById(R.id.sorce);
rd = findViewById(R.id.radioGroup);
button = findViewById(R.id.button);
checkBox1 = findViewById(R.id.cb1);
checkBox2 = findViewById(R.id.cb2);
checkBox3 = findViewById(R.id.cb3);
//為每個復選按鈕設定狀態改變監聽器
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) cb_num++;
else cb_num--;
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) cb_num--;
else cb_num++;
}
});
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) cb_num++;
else cb_num--;
}
});
//設定單選按鈕組添加事件監聽
rd.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//獲取被選擇的單選按鈕
RadioButton r = (RadioButton) findViewById(checkedId);
if(r.getText().equals("開源的")) rd_num = true;
else rd_num = false;
}
});
//edittext監聽
et_on.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(et_on.getText().toString().equals("2")) et_num = true;
else et_num = false;
return false;
}
});
//獲取下拉串列物件
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
//為Spinner添加選擇監聽器
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
//資料選擇事件處理
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String[] Systems = getResources().getStringArray(R.array.Systems);
//顯示選擇結果
if(Systems[pos].equals("Linux")) sp_num = true;
else sp_num = false;
}
//以下方法重寫必須有
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(cb_num == 2) i++;
if(sp_num) i++;
if(et_num) i++;
if(rd_num) i++;
sorce.setText(" "+(i*50)+" ");
if(i == 4)
Toast.makeText(MainActivity.this,"你真棒!祝賀你!", Toast.LENGTH_SHORT).show();
else if(i == 2)
Toast.makeText(MainActivity.this,"成績合格!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"成績不合格!", Toast.LENGTH_SHORT).show();
}
});
}
}

<string name="app_name" translatable="false">zuoye06_231</string>
<string name="di1ti" translatable="false">選擇題</string>
<string name="di2ti" translatable="false">Android作業系統是(50分)</string>
<string name="di3ti" translatable="false">Android Studio 開發Android程式,還需安裝:( )(50分)</string>
<string name="subscribe" translatable="false">提交</string>
<string name="di4ti" translatable="false">" "</string>
三、運行結果截圖:
1、(打開程式界面)

2、(回答錯誤界面)

3、(通過考試界面)

到此本次實驗完成!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/234926.html
標籤:其他
上一篇:HTML如何引入EL和Layui框架(附上代碼和實作)
下一篇:Androidstudio 4.1 找不到符號: 變數 VERSION_NAME 位置: 類 BuildConfig
