最近刚毕业,马上要入职新公司了,方向为Android framework,开始简单复习一下吧。
希望也能给刚开始学习android的同学提供一点帮助。
activity_main.xml
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("被点击");
}
}
}



