Android開發基礎入門、進階系列文章正陸續和大家發布分享,感興趣的小伙伴可以關注博主!一起探討和學習Android技術,助你在編程道路上一臂之力!
目錄
一、RadioButton單選框
二、CheckBox復選框
三、Spinner下拉框
四、ListView串列框
五、在xml檔案中為下拉框和串列框設定引數
Hello,你好呀,我是灰小猿,一個超會寫bug的程式猿!
最近在進行Android方向的學習,所以今天在這里和大家總結一下在Android開發中最經常使用的單選框、復選框、下拉框、串列框的詳細使用教程,
之后還會更新更多有關Android入門的技術供大家學習,所以歡迎小伙伴們關注我一起學習呀!
一、RadioButton單選框
單選框RadioButton的使用是建立在RadioGroup中的,原因是因為我們知道單選框的選擇是互斥的,也就是說只能選擇一個選項,那么如何做到單選框選項的互斥呢?
RadioGroup就起到了作用,在RadioGroup中的單選框選項RadioButton會自動形成互斥,以至于在其中的選項執行選擇一個,
判斷某個單選框是否被選中使用的是isChecked()方法,當該單選框被選中的時候回傳true,否則回傳false,
下面我們通過一個實際的案例來介紹單選框的具體使用,選擇性別并通過按鈕提交之后,在后臺可以獲取到選中的內容,并回傳前端界面訊息框顯示選中內容,
在XML檔案中建立一個單選框界面,進行性別的選擇,大家可以看一下其中的單選框RadioButton是放置在哪里的
<?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/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="單選框使用"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/title_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/title1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/title_sex"
app:layout_constraintBottom_toBottomOf="@id/title_sex"
android:orientation="horizontal"
android:layout_marginTop="30dp"
>
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="男"
/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="女"
/>
</RadioGroup>
<Button
android:id="@+id/tijiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30dp"
android:layout_marginTop="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/title_sex"
></Button>
</androidx.constraintlayout.widget.ConstraintLayout>
獲取單選框選中內容的思路是:設定一個引數接收選中內容,之后判斷每一個單選框是否被選中,如果選中則將內容賦值給引數,
MainActivity.java檔案的代碼如下:
package com.example.radio_checkbox;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取到三個控制元件的屬性
Button bt1 = findViewById(R.id.tijiao);
final RadioButton men = findViewById(R.id.man);
final RadioButton women = findViewById(R.id.woman);
//為提交按鈕添加事件監聽
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sex = ""; //定義sex來接收單選框選擇的內容
if (men.isChecked())sex="男"; //如果點擊了男
else if (women.isChecked()) sex="女"; //如果點擊了女
else sex="未選擇"; //如果未點擊
//將選擇資訊在提示框輸出
Toast.makeText(MainActivity.this,"性別是:" + sex,Toast.LENGTH_LONG).show();
}
});
}
}
選中并提交后的效果如下:

二、CheckBox復選框
我們知道復選框的選項是可以被多個選中,那么很顯然,復選框的選項一定不是像單選框那樣存在互斥現象,因此復選框控制元件不需要放置在某一個容器中,而是直接設定即可,
判斷某個復選框是否被選中使用的是同樣也是isChecked()方法,當該復選框被選中的時候回傳true,否則回傳false,
下面我們通過一個實際的案例來介紹復選框的具體使用,選擇愛好并通過按鈕提交之后,在后臺可以獲取到選中的多個內容,并回傳前端界面訊息框顯示所有的選中內容,
在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/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="復選框使用"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/title_aihao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的愛好:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/title1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="30dp"
/>
<CheckBox
android:id="@+id/lanqiu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="籃球"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/title_aihao"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
/>
<CheckBox
android:id="@+id/paiqiu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排球"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/title_aihao"
app:layout_constraintLeft_toRightOf="@id/lanqiu"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
/>
<CheckBox
android:id="@+id/pingpangqiu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/title_aihao"
app:layout_constraintLeft_toRightOf="@id/paiqiu"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
/>
<Button
android:id="@+id/tijiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30dp"
android:layout_marginTop="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/lanqiu"
></Button>
</androidx.constraintlayout.widget.ConstraintLayout>
獲取復選框選中內容的思路是:設定一個引數接收選中內容,之后判斷每一個復選框是否被選中,如果選中則將內容給增加給引數,最后所有的復選框都判斷完之后,引數中就存放了所有的選中項的文本,
MainActivity.java檔案的代碼如下:
package com.example.radio_checkbox;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取到四個控制元件的屬性
Button bt1 = findViewById(R.id.tijiao);
final CheckBox laiqiu = findViewById(R.id.lanqiu);
final CheckBox paiqiu = findViewById(R.id.paiqiu);
final CheckBox pingpangqiu = findViewById(R.id.pingpangqiu);
//為提交按鈕添加事件監聽
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String info = ""; //定義sex來接收單選框選擇的內容
if (laiqiu.isChecked()) info += "籃球"; //如果點擊了籃球
if (paiqiu.isChecked()) info += "排球"; //如果點擊了排球
if (pingpangqiu.isChecked()) info += "乒乓球"; //如果點擊了乒乓球
//將選擇資訊在提示框輸出
Toast.makeText(MainActivity.this,"您的愛好是:" + info,Toast.LENGTH_LONG).show();
}
});
}
}
選中并提交后的效果如下:

三、Spinner下拉框
Spinner下拉框的使用是需要建立Spinner控制元件的,那么肯定就會有小伙伴問了,Spinner中的選項如何添加,別急!與html中的下拉框添加文本的方式不同,Spinner下拉框的選擇文本是不在Spinner控制元件中寫入的,而是單獨的寫在陣列或一個xml檔案中,在這里先給大家介紹使用陣列存放Spinner下拉框的文本內容的方法,xml存放的方法待會介紹,
首先是先在xml界面中生成一個Spinner下拉框控制元件,
我們要實作的效果同樣是選中顯示的城市,然后通過按鈕提交之后,在后臺可以獲取到選中的內容,并回傳前端界面訊息框顯示選中的內容,
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/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇你的城市:"
android:textSize="20dp"
></TextView>
<Spinner
android:id="@+id/sp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/text1"
android:layout_marginTop="10dp"
></Spinner>
<Button
android:id="@+id/tijiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@id/sp1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
></Button>
</androidx.constraintlayout.widget.ConstraintLayout>
利用陣列設定Spinner下拉框內容的方法是:
-
建立一個string陣列,其中存放每一個選項,
-
將陣列添加到ArrayAdapter容器中,該ArrayAdapter的作用就是存放陣列元素
-
使用Spinner的setAdapter()方法將ArrayAdapter添加給下拉框,完成元素的添加
獲取下拉框選中內容的思路是:設定一個引數接收選中內容,使用Spinner的getSelectedItem().toString()方法獲取到選中內容賦值給引數,
MainActivity.java檔案的代碼如下:
package com.example.mylisttest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String [] data = {"鄭州","深圳","北京"}; //串列框的選項
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//將選項加入到特定的容器中
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,data);
//獲取到控制元件的屬性
Button bt1 = findViewById(R.id.tijiao);
final Spinner sp1 = (Spinner)findViewById(R.id.sp1);
//將選項加入下拉框
sp1.setAdapter(arrayAdapter);
//為提交按鈕添加事件監聽
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String info = "";
info = sp1.getSelectedItem().toString();
//將選擇資訊在提示框輸出
Toast.makeText(MainActivity.this,"您的城市是:" + info,Toast.LENGTH_LONG).show();
}
});
}
}
選中并提交后的效果如下:

四、ListView串列框
ListView串列框的使用和設定其實和Spinner下拉框差不多,他們的選擇引數都是在控制元件之外設定的,同樣的ListView串列框的選擇引數也是單獨的寫在陣列或一個xml檔案中,不同的地方是ListView串列框只能點擊不能選中,所以我們需要單獨給ListView串列框設定監聽函式,
我們以設定ListView串列框,并在用戶點擊串列框某個選項時在界面訊息框中顯示該內容,
下面先在xml界面中實作ListView串列框:
<?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/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇你的城市:"
android:textSize="20dp"
></TextView>
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/text1"
android:layout_marginTop="30dp"
></ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
監聽ListView串列框選中的方法是:為ListView串列框設定setOnItemClickListene()監聽方法,并使用其中的parent.getItemAtPosition(position)方法來獲取點擊的串列框,并獲取到文本,
MainActivity.java檔案的代碼如下:
package com.example.mylisttest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String [] data = {"鄭州","深圳","北京"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,data);
//獲取到串列框的屬性
ListView lv1 = (ListView)findViewById(R.id.lv1);
lv1.setAdapter(arrayAdapter); //將陣列中存放的資訊添加到串列框中
// 為串列框增加監聽事件
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//獲取到串列框點擊的內容
String strInfo = parent.getItemAtPosition(position).toString().trim();
//將內容在訊息框輸出
Toast.makeText(MainActivity.this,strInfo,Toast.LENGTH_LONG).show();
}
});
}
}
選中并提交后的效果如下:

五、在xml檔案中為下拉框和串列框設定引數
在上面的方法中,我們已經介紹了使用陣列為下拉框和串列框設定引數,同樣我們也提到了使用xml檔案為下拉框和串列框設定引數,那么我們現在就來講一下,如何在xml檔案中為下拉框和串列框設定引數,這種方法也是之后經常使用的設定引數方法,
我們以下拉框為例進行設定,串列框方法一樣,使用該方法設定引數的優點就是省去了陣列元素需要添加到ArrayAdapter容器中繁瑣,并且方便之后在xml檔案中添加選擇項,首先我們應該在專案中res—>values檔案下建立一個xml檔案,我在這里命名為array.xml,

之后在xml檔案中寫入我們的要設定的選中引數:如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="array2">
<item>煙臺</item>
<item>鄭州</item>
<item>青島</item>
<item>上海</item>
</string-array>
</resources>
添加完成后直接在下拉框的xml界面檔案中,找到設定下拉框的代碼,在其中添加android:entries屬性即可,界面代碼如下:
<?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/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇你的城市:"
android:textSize="20dp"
></TextView>
<Spinner
android:id="@+id/sp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/text1"
android:layout_marginTop="10dp"
android:entries="@array/array2"
></Spinner>
<Button
android:id="@+id/tijiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@id/sp1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
></Button>
</androidx.constraintlayout.widget.ConstraintLayout>
這樣在MainActivity.java檔案下就不需要設定陣列和對應的容器屬性,直接使用監聽即可,
MainActivity.java檔案的代碼如下:
package com.example.mylisttest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取到控制元件的屬性
Button bt1 = findViewById(R.id.tijiao);
final Spinner sp1 = (Spinner)findViewById(R.id.sp1);
//為提交按鈕添加事件監聽
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String info = "";
info = sp1.getSelectedItem().toString();
//將選擇資訊在提示框輸出
Toast.makeText(MainActivity.this,"您的城市是:" + info,Toast.LENGTH_LONG).show();
}
});
}
}
效果如下:

關于android中單選框、復選框、下拉框、串列框的使用就介紹完了,其中有不足的地方歡迎小伙伴評論指出!
之后還會繼續和大家分享更多Android入門及進階技術,感興趣的小伙伴可以關注我一起學習!
覺得有用記得點贊收藏加關注呀!
灰小猿陪你一起進步!

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/279351.html
標籤:其他
上一篇:Codeforces Round #717 (Div. 2) C Baby Ehab Partitions Again(思維)
