如何使用 Android 中的單選按鈕?
此示例演示瞭如何在 Android 中使用單選按鈕。
步驟 1 − 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新專案,並填寫所有必需資訊以建立一個新專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RadioGroup android:id="@+id/radioGender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radioMale" android:checked="true" /> <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radioFemale" /> </RadioGroup> <Button android:layout_below="@id/radioGender" android:id="@+id/btnDisplay" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnDisplay" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerButton();
}
private void addListenerButton() {
radioGroup = findViewById(R.id.radioGender);
button = findViewById(R.id.btnDisplay);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedID = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(selectedID);
Toast.makeText(MainActivity.this, radioButton.getText(),Toast.LENGTH_SHORT).show();
}
});
}
}步驟 4 − 開啟 res/values/strings.xml 並新增以下程式碼
<resources> <string name="app_name">ample</string> <string name="radioMale">Male</string> <string name="radioFemale">Female</string> <string name="btnDisplay">Display</string> </resources>
步驟 5 − 將以下程式碼新增到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行你的應用程式。我假設你已將你的實際 Android 移動裝置連線到計算機。要在 Android Studio 中執行應用,開啟一個專案的活動檔案,然後單擊工具欄中的執行
圖示。選擇你的移動裝置作為選項,然後檢視將顯示預設螢幕的移動裝置 −

點選此處 下載 專案程式碼。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP