• 今日所学—Android中简单控件的使用


     

     

     

     

     

     下面是相关的代码:

    activity_layout.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/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="212dp"
            android:layout_marginLeft="212dp"
            android:layout_marginTop="76dp"
            android:text="TextView"
            android:textColor="#ff0000"
            android:textSize="30sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="52dp"
            android:layout_marginLeft="52dp"
            android:layout_marginTop="296dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="用户名"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="236dp"
            android:layout_marginLeft="236dp"
            android:layout_marginBottom="144dp"
            android:text="点击"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="76dp"
            android:layout_marginLeft="76dp"
            android:layout_marginTop="34dp"
            android:checked="true"
            android:text="CheckBox"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText" />
    
        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="79dp"
            android:layout_marginLeft="79dp"
            android:layout_marginTop="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/checkBox">
    
            <RadioButton
                android:id="@+id/radioButton"
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:text="RadioButton" />
    
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="RadioButton" />
    
        </RadioGroup>
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="128dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars[0]" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>

    MainActivity.java

    package com.example.kongjian;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import static android.icu.text.DisplayContext.LENGTH_SHORT;
    
    public class MainActivity extends AppCompatActivity {
        private ImageView imageView;
        private TextView textView;
        private EditText editText;
        private RadioGroup radioGroup;
        private CheckBox checkBox;
        private Button button;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=findViewById(R.id.button);
            imageView=findViewById(R.id.imageView);
            textView=findViewById(R.id.textView);
            editText=findViewById(R.id.editText);
            radioGroup=findViewById(R.id.radioGroup);
            checkBox=findViewById(R.id.checkBox);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String edit=editText.getText().toString();
                    textView.setText(edit);
                    imageView.setImageResource(R.drawable.ic_launcher_background);
    
                }
            });
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId==R.id.radioButton){
                        Toast.makeText(getApplicationContext(),"1已选择",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getApplicationContext(),"2已选择",Toast.LENGTH_SHORT).show();
                    }
                }
            });
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        Toast.makeText(getApplicationContext(),"已选择",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getApplicationContext(),"没有选择",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    效果截图:

  • 相关阅读:
    守护线程与普通线程
    Java中实现多线程的两种方式之间的区别
    Python os.dup() 方法
    Python os.closerange() 方法
    Python os.close() 方法
    Python os.chroot() 方法
    Python os.chmod() 方法
    JavaScript——基本包装类型
    javascript——function类型(this关键字)
    常用的正则
  • 原文地址:https://www.cnblogs.com/MoooJL/p/12292329.html
Copyright © 2020-2023  润新知