补10
目錄
- 涉及知識點
- 創建安卓應用
- 將背景圖片拷貝到drawable目錄
- 主布局資源文件
- 字符串資源文件strings.xml
- 主界面類
- 查看效果
涉及知識點
1、線性布局(LinearLayout)
2、標簽(TextView)
3、按鈕(Button)
4、編輯框(EditText)
5、單選按鈕組(RadioGroup)
6、單選按鈕(RadioButton)
7、復選框(CheckBox)
創建安卓應用
將背景圖片拷貝到drawable目錄
主布局資源文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".setbasic"android:background="@drawable/tt"android:orientation="vertical"android:paddingLeft="20dp"android:paddingRight="20dp"android:paddingTop="30dp"><TextViewandroid:id="@+id/tvSetInformation"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginBottom="30dp"android:text="@string/set_information"android:textColor="#0000ff"android:textSize="30sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:id="@+id/tvName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/Name"android:textColor="#000000"android:textSize="16sp" /><EditTextandroid:id="@+id/edtName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="@string/input_name"android:singleLine="true" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:id="@+id/tvGender"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/gender"android:textColor="#000000"android:textSize="16sp" /><RadioGroupandroid:id="@+id/rgGender"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rbMale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="@string/male" /><RadioButtonandroid:id="@+id/rbFemale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:text="@string/female" /></RadioGroup></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:id="@+id/tvHobby"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hobby"android:textColor="#000000"android:textSize="16sp" /><CheckBoxandroid:id="@+id/cbMusic"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/music" /><CheckBoxandroid:id="@+id/cbRead"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/read" /><CheckBoxandroid:id="@+id/cbFood"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/food" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"><Buttonandroid:id="@+id/btnOk"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="doOK"android:text="@string/Ok" /><Buttonandroid:id="@+id/btnClear"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="doClear"android:text="@string/clear" /><Buttonandroid:id="@+id/btnExit"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="doExit"android:text="@string/exit" /></LinearLayout><TextViewandroid:id="@+id/tvResult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:textSize="15sp" /></LinearLayout>字符串資源文件strings.xml
<resources><string name="set_information">設置基本信息</string><string name="Name">姓名:</string><string name="input_name">請輸入姓名</string><string name="gender">性別:</string><string name="male">男</string><string name="female">女</string><string name="hobby">愛好:</string><string name="travel">旅行</string><string name="read">閱讀</string><string name="food">美食</string><string name="music">音樂</string><string name="Ok">確定</string><string name="clear">清除</string><string name="exit">退出</string></resources>主界面類
package net.tp.xiangduibuju;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast;public class setbasic extends AppCompatActivity {private EditText edtName;private RadioGroup rgGender;private RadioButton rbMale;private RadioButton rbFemale;private CheckBox cbRead;private CheckBox cbMusic;private CheckBox cbFood;private TextView tvResult;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 利用布局資源文件設置用戶界面setContentView(R.layout.activity_setbasic);//通過資源標識符獲得控件實例edtName=findViewById(R.id.edtName);rgGender=findViewById(R.id.rgGender);rbMale=findViewById(R.id.rbMale);rbFemale=findViewById(R.id.rbFemale);cbRead=findViewById(R.id.cbRead);cbMusic=findViewById(R.id.cbMusic);cbFood=findViewById(R.id.cbFood);tvResult=findViewById(R.id.tvResult);}/** 提交按鈕單擊事件處理方法* @param view*/public void doOK(View view){//獲取姓名String strName=edtName.getText().toString().trim();//獲取性別值String strGender="";//根據選中單選按鈕的Id進行判斷switch (rgGender.getCheckedRadioButtonId()){case R.id.rbMale:strGender=rbMale.getText().toString();break;case R.id.rbFemale:strGender=rbFemale.getText().toString();break;}//獲取愛好StringBuilder builder =new StringBuilder();if(cbRead.isChecked()){builder.append(cbRead.getText().toString()+" ");}if (cbMusic.isChecked()){builder.append(cbMusic.getText().toString()+" ");}if (cbFood.isChecked()){builder.append(cbFood.getText().toString()+" ");}String strHobby = builder.toString().trim();//顯示基本信息if (!(strName.equals("")) && strHobby != "") {String result = "姓名:" + strName + "\n性別:" + strGender + "\n愛好:" + strHobby;tvResult.setText(result);} else {Toast.makeText(setbasic.this, "基本信息不全,請填寫完全后再提交",Toast.LENGTH_SHORT).show();}}public void doClear(View view){edtName.setText("");rbMale.setChecked(true);cbRead.setChecked(false);cbMusic.setChecked(false);cbFood.setChecked(false);tvResult.setText("");}public void doExit(View view){finish();} }查看效果
總結
- 上一篇: 带鱼怎么处理干净,带鱼处理干净的方法?
- 下一篇: 相互宝年龄的限制