第三单元学校里所讲控件

第三单元学校里所讲控件

1.ImageView图片

考点1:src和background

background是背景图片

当设置长宽matchparent

background会铺满

而src不会,他会按原图的比例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
在LinearLayout里记得要设orientation
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher"/>
</LinearLayout>
小知识点

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello android"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
缩放拖拉
android:scaleType="fitEnd"
/>

</LinearLayout>

2.EditText编辑框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"现在这一种写法被淘汰
应该写成
android:inputType="numberPassword"

/>
</LinearLayout>

3.RadioButton单选框

这个app功能是:

第一个文本框会显示当前学历

第二个文本框会显示性别

第三个文本框显示这两项总和

第一个文本框考的是匿名内部类+接口+事件监听(关注的是选项变化setOnCheckedChangeListener,不是很重要一般我们只关注结果)

第二个文本框考察的是普通按钮都有的onClick方法,触发谁进行谁。

对于第二个文本框那种操作函数必须是public void

第三个文本框综合框,考察字符串拼接:先初始化String pre=“”;String aft ="";前缀存学历,后缀存性别,都先将他们初始化为空。

然后利用一个非常重要的函数isChecked

最后对最后一个文本框setText字符串拼接前缀+“,“+后缀

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/pg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Program"/>
<RadioGroup
android:id="@+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/ud"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Undergraduate"/>
<RadioButton
android:id="@+id/mas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Master"/>
<RadioButton
android:id="@+id/phd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PhD"/>

</RadioGroup>
<TextView
android:id="@+id/gen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"/>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male"
android:onClick="maleclick"
/>
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female"
android:onClick="femaleclick"

/>
MainActivity.java

</RadioGroup>
<TextView
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TBD"/>
<Button

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:onClick="sumclick"
/>

</LinearLayout>
 
 
 
 
 
package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
TextView gen;
TextView pg;
RadioButton ud,mas,phd,male,female;
RadioGroup rg1,rg2;
TextView sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gen=findViewById(R.id.gen);
pg=findViewById(R.id.pg);
ud=findViewById(R.id.ud);
mas=findViewById(R.id.mas);
phd=findViewById(R.id.phd);
male=findViewById(R.id.male);
female=findViewById(R.id.female);
rg1=findViewById(R.id.rg1);
rg2=findViewById(R.id.rg2);
sum=findViewById(R.id.sum);
//匿名内部类+接口+监听
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.ud){
pg.setText("Undergraduate");
}
else if(checkedId==R.id.mas){
pg.setText("Master");
}
else if(checkedId==R.id.phd){
pg.setText("PhD");
}
}
});
}
public void maleclick(View v){
gen.setText("Male");
}
public void femaleclick(View v){
gen.setText("FeMale");
}
public void sumclick(View v){
String pre="";
String aft="";
if(ud.isChecked()){
pre="undergraduate";
}
else if(mas.isChecked()){
pre="Master";
}
else if(phd.isChecked()){
pre="PhD";
}
if(male.isChecked()){
aft="Male";
}
else if(female.isChecked()){
aft="Female";
}
sum.setText(pre+","+aft);
}
}
 

CheckBox 复选框

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="请选择爱好:"
    android:textColor="#FF8800"

    />
   <CheckBox
       android:id="@+id/ch1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="羽毛球"/>
    <CheckBox
        android:id="@+id/ch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球"/>
    <CheckBox
        android:id="@+id/ch3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="足球"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="您选择的爱好是:"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hobby"/>

</LinearLayout>

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hobby"/>
这是为了最后显示一共选了哪几项运动,开始是空白等待填充!
 
 
MainActivity.java
package com.example.checkbox1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox ch1,ch2,ch3;
TextView hobby;//最终显示所选运动
String hobbies;//存放所选运动的内容
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //第一步:实例化,找到这些控件
ch1=findViewById(R.id.ch1);
ch2=findViewById(R.id.ch2);
ch3=findViewById(R.id.ch3);
hobby=findViewById(R.id.hobby);
hobbies=new String();
//第二步:设置监听器
        ch1.setOnCheckedChangeListener(this);
        ch2.setOnCheckedChangeListener(this);
        ch3.setOnCheckedChangeListener(this);

    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String name=buttonView.getText().toString();//获取当前状态改变控件的名称
        if(isChecked){//选中
            if(!hobbies.contains(name)){//但是现在所存选中内容不包含这个控件
                hobbies+=name;
                hobby.setText(hobbies);
            }

        }
        else{//未选中
            if(hobbies.contains(name)){//未选中,但是选中内容里面有这个控件,那就要删除他用replace
                
                hobbies=hobbies.replace(name,"");
                hobby.setText(hobbies);
            }

        }
    }
}
 
状态改变监听器,只有状态改变才触发
 ch1.setOnCheckedChangeListener(this);
填上this他会红色,那么点住this右键先选第二个,再选第一个。
 

总结

以上题目有个做题思路

(在onCreate方法里:)

第一步实例化findViewById找到要操纵的控件

第二步:设置监听器set...Listener
(写监听方法)
第三步:public void 监听方法
(进行完第二步系统自动会有第三步只需要我们自定义方法体!)
 

综合应用

 

main.java
package com.example.myapplication111;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
TextView pg,gen,course,sum;
RadioButton ud,mas,phd,male,female;
CheckBox ch1,ch2,ch3;
RadioGroup rg1,rg2;
String c;
String a;
String b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pg=findViewById(R.id.pg);
        gen=findViewById(R.id.gen);
        course=findViewById(R.id.course);
        sum=findViewById(R.id.sum);
        ud=findViewById(R.id.ud);
        mas=findViewById(R.id.mas);
        phd=findViewById(R.id.phd);
        male=findViewById(R.id.male);
        female=findViewById(R.id.female);
        ch1=findViewById(R.id.ch1);
        ch2=findViewById(R.id.ch2);
        ch3=findViewById(R.id.ch3);
        rg1=findViewById(R.id.rg1);
        rg2=findViewById(R.id.rg2);
        rg1.setOnCheckedChangeListener(this);
        ch1.setOnCheckedChangeListener(this);
        ch2.setOnCheckedChangeListener(this);
        ch3.setOnCheckedChangeListener(this);
        c=new String();
        a=new String();
        b=new String();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(checkedId==R.id.ud) {
            pg.setText("undergraduate");
a="undergraduste";

        }
        else if(checkedId==R.id.mas) {
            pg.setText("master");
a="master";

        }
        else if(checkedId==R.id.phd){pg.setText("phd");
            a="phd";}
    }
    public  void maleclick(View v){
        gen.setText("male");
        b="male";

    }
    public void femaleclick(View v){
        gen.setText("female");
        b="female";
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String n=buttonView.getText().toString();
        if(isChecked){//选中
            if(!c.contains(n)){
                c+=n;
                course.setText(c);
            }
        }
        else{//没选中
            if(c.contains(n)){
                c=c.replace(n,"");
                course.setText(c);
            }
        }
    }
    public void sumclick(View v){
sum.setText(a+","+b+","+c);
    }
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/pg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Program"/>
    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/ud"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Undergraduate"/>
        <RadioButton
            android:id="@+id/mas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Master"/>
        <RadioButton
            android:id="@+id/phd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="PhD"/>

    </RadioGroup>
    <TextView
        android:id="@+id/gen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender"/>
    <RadioGroup
        android:id="@+id/rg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/male"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Male"
            android:onClick="maleclick"
            />
        <RadioButton
            android:id="@+id/female"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Female"
            android:onClick="femaleclick"

            />
    </RadioGroup>
    <TextView
        android:id="@+id/course"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Course"
        />
    <CheckBox
        android:id="@+id/ch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Java"/>
    <CheckBox
        android:id="@+id/ch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data Structure"
        />
    <CheckBox
        android:id="@+id/ch3"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ComputerNetwork"/>

    <TextView
        android:id="@+id/sum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TBD"/>
    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:onClick="sumclick"
        />

</LinearLayout>

总结:

1.Button,RadioButton,CheckBox都有onclick属性这种属性在xml只用写 android:onClick="方法名"就能进行点击事件

在java文件里面写方法public void 方法名(View v){

方法体}

2.监听器

(1)findViewById

(2)

setOnCkeckChangedListener

(3)public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 

Toast 消息提示框

动态,在java文件里定义,不用在xml里面定义!

出现位置:页面最下方

定义方式

Toast.makeText(MainActivity.this, "要显示的内容", Toast.LENGTH_LONG).show();
 
 
Toast.LENGTH_LONG和Toast.LENGTH_SHORT可以二选一(表示显示时间长度)

 

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>
<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="姓名"/>
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"/>
    <EditText
        android:id="@+id/ps"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:onClick="send"
        />



</LinearLayout>
mainactivity.java
 package com.example.toa;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
EditText name,ps;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=findViewById(R.id.name);
        ps=findViewById(R.id.ps);
    }
    public void send(View v){
if(name.getText().toString().equals("wmy") && ps.getText().toString().equals("123456")){
    Toast.makeText(MainActivity.this, "success", Toast.LENGTH_LONG).show();
}
else {
    Toast.makeText(MainActivity.this, "unsuccess", Toast.LENGTH_LONG).show();
}
    }
}

显示效果:

 

这个项目是:只有当输入姓名是wmy且密码是123456时Toast输出success否则输出unsuccess

滚动条

 

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="match_parent"
    android:layout_width="match_parent"

    >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="helloworld"
        android:textSize="20sp"
        />
</LinearLayout>


</ScrollView>

posted @   Annaprincess  阅读(4)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示