Android点击事件、下拉菜单、单选框实例
点击事件
1.ClickActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ClickActivity extends Activity {
private Button bt1;
private Button bt2;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_click);
bt1=(Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv1=(TextView) findViewById(R.id.test);
tv1.setText("这是监听事件");
}
});
}
// @Override
// public void onClick(View v){
// }
public void myClick(View v){
tv1=(TextView) findViewById(R.id.test);
tv1.setText("这是函数点击事件");
}
}
2.activity_click.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="几种点击事件的设置方法"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="---------测试文本-----------"
android:gravity="center"/>
<TextView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="通过监听器"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:text="通过设置额外函数"
android:onClick="myClick"/>
</LinearLayout>
效果图:
单选框
1.RadioGroupActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class RadioGroupActivity extends Activity {
private RadioGroup sex;
private RadioButton man;
private RadioButton woman;
private RadioButton dog;
private TextView text;
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
super.setContentView(R.layout.activity_radio);
sex=(RadioGroup) findViewById(R.id.sex);
man=(RadioButton)findViewById(R.id.man);
woman=(RadioButton)findViewById(R.id.woman);
dog=(RadioButton)findViewById(R.id.dog);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group,int checkedId){
RadioButton ch=(RadioButton)findViewById(checkedId);
text=findViewById(R.id.text);
if(ch==man){
text.setText("男人");
}else if (ch==woman){
text.setText("女人");
}else{
text.setText("狗");
}
}
});
}
2.activity_radio
<?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">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择你的性别"/>
<RadioGroup
android:id="@+id/sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@id/dog">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/man"
android:text="男人"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/woman"
android:text="女人"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dog"
android:text="狗"/>
</RadioGroup>
效果图:
下拉菜单
1.SpinnerActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class SpinnerActivity extends Activity {
private Spinner Scolor;
private Spinner Scity;
private Spinner Sfruit;
private Spinner Sanimal;
private TextView demo;
List<CharSequence> dataAnimal;
private ArrayAdapter<CharSequence> adapterFruit;
private ArrayAdapter<CharSequence> adapterAnimal;
@Override
public void onCreate(Bundle saveInstanceStates){
super.onCreate(saveInstanceStates);
super.setContentView(R.layout.activity_spinner);
this.Scolor=findViewById(R.id.color);
this.Scity=findViewById(R.id.city);
this.Sfruit=findViewById(R.id.fruit);
this.Sanimal=findViewById(R.id.animal);
this.demo=findViewById(R.id.demo);
this.Scity.setPrompt("选城市");
this.Scolor.setPrompt("选颜色");
this.Sfruit.setPrompt("选水果");
this.Sanimal.setPrompt("选动物");
// 设置之后不起作用:prompt属性只有在dialog状态才有用,所以要在xml中,将style设置为Widget.Spinner
// prompt属性要用string下资源,不支持字符直接输入,否则会报错误
this.adapterFruit=ArrayAdapter.createFromResource(this,R.array.fruit_label,android.R.layout.simple_spinner_item);
this.adapterFruit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.Sfruit.setAdapter(adapterFruit);
this.dataAnimal=new ArrayList<CharSequence>();
this.dataAnimal.add("柴犬");
this.dataAnimal.add("猫");
this.dataAnimal.add("大象");
this.dataAnimal.add("蟒蛇");
this.dataAnimal.add("犀牛");
this.adapterAnimal=new ArrayAdapter(this,android.R.layout.simple_spinner_item,this.dataAnimal);
this.adapterAnimal.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.Sanimal.setAdapter(this.adapterAnimal);
this.Scity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,long l){
String info=adapterView.getItemAtPosition(i).toString();//获得i所在的文本
demo.setText(info);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
this.Scolor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,long l){
String info=adapterView.getItemAtPosition(i).toString();//获得i所在的文本
switch (info){
case "红色":demo.setTextColor(0xFF0000FF);
break;
case "黑色":demo.setTextColor(0xFFFFFFFF);
break;
case "黄色":demo.setTextColor(0xADFFA0FF);
break;
case "蓝色":demo.setTextColor(0x7B68EEFF);
break;
case "绿色":demo.setTextColor(0x7CFC00FF);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
2.activity_spinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/demo"
android:text="测试样例"
android:gravity="center"
android:textSize="80px"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择你喜欢的城市"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/city"
android:entries="@array/city_labels" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择你喜欢的城市颜色"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/color"
android:entries="@array/color_labels" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择你喜欢的水果"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fruit"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择你喜欢的动物"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/animal"/>
</LinearLayout>
效果图:
项目结构
本篇文章,仅做本人的Android学习路程记录作用,如果能帮到各位再好不过,若有错误也希望大佬们指出!!!