最近刚毕业,马上要入职新公司了,方向为Android framework,开始简单复习一下吧。
希望也能给刚开始学习android的同学提供一点帮助。
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">
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一"
app:layout_constraintBottom_toTopOf="@+id/btn_two"
app:layout_constraintEnd_toEndOf="@+id/btn_two"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/btn_two"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.877" />
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:onClick="click"
android:text="按钮二"
app:layout_constraintBottom_toTopOf="@+id/btn_three"
app:layout_constraintEnd_toEndOf="@+id/btn_three"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/btn_three" />
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="408dp"
android:text="按钮三"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.476"
app:layout_constraintStart_toStartOf="parent" />
<RadioGroup
android:id="@+id/rdg"
android:layout_width="257dp"
android:layout_height="75dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:id="@+id/rbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TextView
android:id="@+id/tv"
android:layout_width="147dp"
android:layout_height="28dp"
android:layout_marginTop="24dp"
android:text="您输入的性别为:"
app:layout_constraintEnd_toEndOf="@+id/rdg"
app:layout_constraintStart_toStartOf="@+id/rdg"
app:layout_constraintTop_toBottomOf="@+id/rdg" />
<CheckBox
android:id="@+id/like_shuttlecock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="羽毛球"
app:layout_constraintBottom_toBottomOf="@+id/rdg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.546" />
<CheckBox
android:id="@+id/like_basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="篮球"
app:layout_constraintEnd_toEndOf="@+id/like_shuttlecock"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/like_shuttlecock"
app:layout_constraintTop_toBottomOf="@+id/like_shuttlecock" />
<CheckBox
android:id="@+id/like_pingpang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="乒乓球"
app:layout_constraintEnd_toEndOf="@+id/like_basketball"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/like_basketball"
app:layout_constraintTop_toBottomOf="@+id/like_basketball" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择爱好"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.218"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.509" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="88dp"
android:text="您选择的爱好为:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.785" />
<TextView
android:id="@+id/hobby"
android:layout_width="107dp"
android:layout_height="17dp"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.252"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package cn.itcast.myapplication;
import android.app.AlertDialog;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private Button btn_one, btn_two, btn_three;
private RadioGroup radioGroup;
private TextView textView;
private TextView hobby;
private String hobbys;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
btn_one = (Button) findViewById(R.id.btn_one) ;
btn_two = (Button) findViewById(R.id. btn_two) ;
btn_three=(Button) findViewById(R.id.btn_three);
radioGroup=(RadioGroup) findViewById(R.id.rdg);
textView=(TextView)findViewById(R.id.tv);
CheckBox shuttlecock=(CheckBox)findViewById(R.id.like_shuttlecock);
CheckBox basketball=(CheckBox)findViewById(R.id.like_basketball);
CheckBox pingpang=(CheckBox)findViewById(R.id.like_pingpang);
shuttlecock.setOnCheckedChangeListener(this);
basketball.setOnCheckedChangeListener(this);
pingpang.setOnCheckedChangeListener(this);
hobby = (TextView)findViewById(R.id.hobby);
hobbys = new String();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId==R.id.rbtn){
textView.setText("你选择了性别男");
}else {
textView.setText("你选择了性别女");
}
}
});
btn_three.setOnClickListener(this);
btn_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn_one.setText("按钮一已被点击");
}
});
}
public void click(View view){
btn_two.setText("按钮二已被点击");
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String motion = buttonView.getText().toString();
if(isChecked) {
if (!hobbys.contains(motion)) {
hobbys = hobbys + motion;
hobby.setText(hobbys);
}
}else {
if (hobbys.contains(motion)){
hobbys=hobbys.replace(motion,"");
hobby.setText(hobbys);
}
}
}
@Override
public void onClick(View v){
// switch (v.getId()){
// case R.id.btn_three:
// btn_three.setText("被点击");
// break;
// }
if (v.getId()==R.id.btn_three){
Toast.makeText(MainActivity.this,"按钮三被点击",Toast.LENGTH_LONG).show();
btn_three.setText("被点击");
}
}
}