Android

xiaoxiao2021-02-28  42

一、ImageView与ImageButton

<ImageView android:layout_width="300dp" android:layout_height="200dp" android:src="@drawable/ic_launcher_background" android:background="#222222" android:scaleType="fitCenter"/>

<ImageView android:layout_width="300dp" android:layout_height="200dp" android:src="@drawable/ic_launcher_background" android:background="#222222" android:scaleType="center"/>

<ImageView android:layout_width="300dp" android:layout_height="200dp" android:src="@drawable/ic_launcher_background" android:background="#222222" android:scaleType="fitXY"/>

在Java代码中更改图片为

imageView.setImageResource(R.drawable.unhappy);

二、CheckBox

public class MainActivity extends AppCompatActivity { private CheckBox cb_game,cb_travel,cb_read; private CheckBoxListener checkBoxListener;//3、定义这个监听器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } //4、 // 在这个方法中实现监听 private void setListener() { checkBoxListener = new CheckBoxListener(); cb_game.setOnCheckedChangeListener(checkBoxListener); cb_travel.setOnCheckedChangeListener(checkBoxListener); cb_read.setOnCheckedChangeListener(checkBoxListener); } //1、为了看着代码干净一点我们用这个初始化一下findviewbyid private void initView() { cb_game=findViewById(R.id.cb_game); cb_travel=findViewById(R.id.cb_travel); cb_read=findViewById(R.id.cb_read); } //2、CheckBoxListener实现OnCheckedChangeListener监听器来监听选择框的改变isChecked当被选中时返回true反之false class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{//注意使用CompoundButton的OnCheckedChangeListener @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //当选种状态发生变化时触发 CheckBox checkBox = (CheckBox) buttonView; switch (checkBox.getId()){ case R.id.cb_game: Toast.makeText(MainActivity.this,"电竞:"+isChecked,Toast.LENGTH_SHORT).show(); break; case R.id.cb_travel: Toast.makeText(MainActivity.this,"旅游:"+isChecked,Toast.LENGTH_SHORT).show(); break; case R.id.cb_read: Toast.makeText(MainActivity.this,"阅读:"+isChecked,Toast.LENGTH_SHORT).show(); break; } } } } <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="请选择爱好:"/> <CheckBox android:id="@+id/cb_game" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="电竞" android:checked="true"/> <CheckBox android:id="@+id/cb_travel" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="旅游"/> <CheckBox android:id="@+id/cb_read" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="阅读"/> </LinearLayout> </LinearLayout>

三、RadioButton

1、RadioButton是圆形单选框

2、RadioGroup是个了以容纳多个RadioButton的容器

3、在RadioGroup中的RadioButton控件可以有多个,但同时有且仅有一个可以被选中

实现单选按钮

    在布局文件中定义RadioGroup

    在RadioGroup中添加RadioButton

    在Java代码中获取控件对象

    为对象添加监听器

<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:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择性别"/> <RadioGroup android:id="@+id/rg_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_Male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男"/> <RadioButton android:id="@+id/rb_FeMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> </LinearLayout> public class MainActivity extends AppCompatActivity { private RadioGroup rg; private RadioButton rb_male,rb_female; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg = findViewById(R.id.rg_sex); rb_male = findViewById(R.id.rb_Male); rb_female = findViewById(R.id.rb_FeMale); rg.setOnCheckedChangeListener(new MyRadioButtonListener()); } class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //在状态改变时被触发 switch (checkedId){ case R.id.rb_FeMale: Log.i("sex","当前用户选择"+rb_female.getText().toString()); break; case R.id.rb_Male: Log.i("sex","当前用户选择"+rb_male.getText().toString()); break; } } } }

四、ToggleButton

<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="你今天开心么?"/> <ToggleButton android:id="@+id/tb" android:layout_width="match_parent" android:layout_height="wrap_content" android:textOff="不开心" android:textOn="开心"/> 按钮按下显示为textOff 抬起显示为textOn public class MainActivity extends AppCompatActivity { private ToggleButton toggleButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggleButton = findViewById(R.id.tb); toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (toggleButton.isChecked()){ Toast.makeText(MainActivity.this,toggleButton.getText().toString(),Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,toggleButton.getText().toString(),Toast.LENGTH_SHORT).show(); } } }); } }

转载请注明原文地址: https://www.6miu.com/read-2622687.html

最新回复(0)