一、单项选择
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择专业"
android:background="#BEC7BE"
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.077" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</RadioGroup>
<RadioButton
android:id="@+id/rb_a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="112dp"
android:text="软件"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/rb_b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="硬件"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rb_a" />
<TextView
android:id="@+id/tv_answer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="180dp"
android:text="显示答案"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btn_submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="71dp"
android:text="提交"
android:background="#4CAF50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rb_b" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.MainActivity.java
package com.example.danxuan;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
public Button btnSubmit;
public RadioButton rbA,rbB;
public TextView tvAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rbA=findViewById(R.id.rb_a);
rbB=findViewById(R.id.rb_b);
btnSubmit=findViewById(R.id.btn_submit);
tvAnswer=findViewById(R.id.tv_answer);
btnSubmit.setOnClickListener(new myClick ());
}
}
3.myClick.javae
package com.example.danxuan;
import android.app.ActionBar;
import android.app.assist.AssistStructure;
import android.os.Build;
import android.view.View;
import androidx.annotation.RequiresApi;
class myClick implements View.OnClickListener {
private ActionBar.Tab tvAnswer;
private AssistStructure.ViewNode rbA;
private AssistStructure.ViewNode rbB;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
CharSequence answer="";
if (rbA.isChecked())
answer=rbA.getText();
if (rbB.isChecked())
answer=rbB.getText();
tvAnswer.setText("答案:"+answer);
}
}
二、切换图片——下一张
1.MainActivity.java
package com.example.elv;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public ImageView ig_Show;
public Button btn_Last, btn_Next;
public int[] images={
R.drawable.hhh1,
R.drawable.hhh2,
R.drawable.hhh3,
R.drawable.hhh4,
R.drawable.hhhh5};
public int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ig_Show = findViewById(R.id.imageView);
btn_Last = findViewById(R.id.button);
btn_Next = findViewById(R.id.button2);
btn_Next.setOnClickListener(this);
btn_Last.setOnClickListener(this);
}
public void onClick(View v) {
if (v == btn_Next) {
if (index <images.length-1)
index++;
else
index=0;
ig_Show.setImageResource(images[index]);
} else if (v == btn_Last) {
if (index>0)
index--;
else
index=images.length-1;
ig_Show.setImageResource(images[index]);
}
}
}
2.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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#8BC34A"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/hhh1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:background="#94919C"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="上一张" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="下一张" />
</LinearLayout>
</LinearLayout>
三、翻译
1.java
package com.example.nine;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView tv_show;
public Button btn_trans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show=findViewById(R.id.textView);
btn_trans=findViewById(R.id.button);
btn_trans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
if(tv_show.getText().equals("hello world"))
tv_show.setText("你好世界");
else
tv_show.setText("hello world");
}
}
});
}
}
2.xml
<?xml version="1.0" encoding="utf-8"?>
<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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world"
android:textSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="161dp"
android:text="翻译"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
四、spinner——选择某个数字
1.xml
<?xml version="1.0" encoding="utf-8"?>
<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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择数字"
android:textColor="#3B6CFF"
android:textSize="20dp"
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.168" />
<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="240dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.java
package com.example.spinnner;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner mSpinner;
private String[] strWeek={"1","2","3","4","5","6","7"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
protected void initView()
{
mSpinner=findViewById(R.id.spinner);
mSpinner.setOnItemSelectedListener(this);
}
protected void initData()
{
ArrayAdapter<String> mArrayAdapter=new ArrayAdapter<String> (this,R.layout.support_simple_spinner_dropdown_item,strWeek);
mSpinner.setPrompt("选择");
mSpinner.setAdapter(mArrayAdapter);
mSpinner.setSelection(0);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String strMsg =String.format("选择第%d个选项:%s",position+1,strWeek[position]);
Toast.makeText(this,strMsg,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
五、布局
1.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button14"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button12"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="Button2"
app:layout_constraintBottom_toTopOf="@+id/button15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button14"
app:layout_constraintVertical_bias="0.434" />
<Button
android:id="@+id/button13"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="208dp"
android:layout_marginLeft="208dp"
android:layout_weight="3"
android:text="Button3"
app:layout_constraintBottom_toTopOf="@+id/button15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button14"
app:layout_constraintVertical_bias="0.434" />
<Button
android:id="@+id/button15"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Button4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button1"
android:layout_centerHorizontal="true"
android:text="上" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_centerHorizontal="true"
android:text="下" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/button1"
android:layout_centerVertical="true"
android:text="左" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:layout_centerVertical="true"
android:text="右" />
</RelativeLayout>
计算器布局
xml
<?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
android:layout_width="match_parent"
android:layout_height="120dp">
<TextView
android:id="@+id/xianhsiping"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:gravity="end|bottom"
android:text="0"
android:padding="10dp"
android:background="#5A8376"
android:textColor="#000000"
android:textSize="60sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:id="@+id/button1qingchu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:gravity="center"
android:text="清除"
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp">
<Button
android:id="@+id/button2jia"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="+"
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
<Button
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="1"
android:textSize="30sp" />
<Button
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="2"
android:textSize="30sp" />
<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="3"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp">
<Button
android:id="@+id/jian"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="-"
android:textSize="30sp" />
<Button
android:id="@+id/four"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="4"
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
<Button
android:id="@+id/five"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="5"
android:textSize="30sp" />
<Button
android:id="@+id/six"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="6"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp">
<Button
android:id="@+id/cehng"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="*"
android:textSize="30sp" />
<Button
android:id="@+id/senven"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="7"
android:textSize="30sp" />
<Button
android:id="@+id/eigt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="8"
android:textSize="30sp" />
<Button
android:id="@+id/nine"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#70B6AEAE"
android:textColor="#070707"
android:gravity="center"
android:text="9"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp">
<Button
android:id="@+id/chu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="/"
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
<Button
android:id="@+id/dian"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="."
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
<Button
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
<Button
android:id="@+id/dengyu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="="
android:background="#70B6AEAE"
android:textColor="#070707"
android:textSize="30sp" />
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:text="设计者:20240108\n嵌入式专业22"
android:textColor="#5572E6"
android:textSize="30dp"
android:gravity="bottom|center_horizontal"/>
</LinearLayout>