Android学习--界面设计

1、创建工程

创建一个Android工程

2、添加相关控件

打开res -> layout下的布局文件,根据实际情况选择一种或多种布局方式,然后分别添加TextView、EditText、RadioGroup、CheckBox、Spinner、ListView和Button控件。其中RadioGroup中加入两个RadioButton控件,分别为“男”和“女”,默认选择“男”;Spinner控件中要加入三个子选项,分别为“计算机”、“软件工程”和“物联网”,“计算机”为默认选项;ListView控件中加入若干姓名

关键代码:

list.add("赵明");
students.add(new Student("赵明","","","","",""));
list.add("李晓");
students.add(new Student("李晓","","","","",""));
list.add("王丽");
students.add(new Student("王丽","","","","",""));

//spinner的数据源
data_list.add("计算机");
data_list.add("软件工程");
data_list.add("物联网");

//适配器
arr_adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,data_list);
arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arr_adapter1);
arr_adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
arr_adapter2.setDropDownViewResource(android.R.layout.simple_list_item_1);
listView.setAdapter(arr_adapter2);

 

3、添加菜单

(1)在工程中添加选项菜单,菜单标题为“设置”,效果如图1.2所示。

        

图1.2 选项菜单

 

关键代码:主要是在res中添加menu文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:id="@+id/conf"
        android:title="设置"
        />
</menu>

并在java文件中添加menu设置信息

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.caidan,menu);
    return true;
}

2为ListView控件添加一个快捷菜单,包含一个“查看”菜单项,效果如图1.3所示

 

代码:

listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("选择操作");
        menu.add(0,0,0,"查看");
        menu.add(0,1,0,"删除");
    }
});

  1. 为“设置”菜单项添加一个“专业设置”子菜单项,效果如图1.4所示。

 

4、操作栏

将步骤3中的选项菜单“设置”添加到操作栏,效果如图1.5所示

 

 

5、界面事件

(1)为步骤2中的“关闭”按钮添加事件,当点击“关闭”按钮时则退出程序。

关键代码:

public void exit(View view){
    System.exit(0);
}

  1. 为步骤2中的“添加”按钮添加事件,当点击“添加”按钮时保存一条基本信息记录,并将该条记录的姓名项加入ListView控件中,如果姓名为空,则不加入ListView控件并给出相应提示,可以利用Toast实现提示。

关键代码:

adding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(name.getText()==null||name.getText().length()==0) {
                    Toast.makeText(getApplicationContext(),"姓名为空!", Toast.LENGTH_SHORT).show();
                }
                else {

                      if(check1.isChecked()){
                          hobby+="旅游  ";
                      }
                      if(check2.isChecked()){
                          hobby+="运动  ";
                      }
                      if(check3.isChecked()){
                          hobby+="其他  ";
                      }
                      if(boy.isChecked()){
                          sex+="男";
                      }
                      else{
                          sex +="女";
                      }
                      onItemSelected();
                      list.add(name.getText().toString());
                      listView.setAdapter(arr_adapter2);
students.add(newStudent(name.getText().toString(),height.getText().toString(),weight.getText().toString(),sex,hobby,spinner_id));            Toast.makeText(getApplicationContext(),students.get(num).speak(),Toast.LENGTH_SHORT).show();
                      num++;
                      sex="";
                      hobby="";
                }
            }
        });

 

  1. 添加按键事件,图1.1的“身高”和“体重”中只能输入数字和字符“.”,无法输入其他字符,当输入其他字符要给出相应提示,可以利用Toast实现提示。

关键代码:

//设置heght监听器 通过键盘按键事件
    height.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode>=7&&keyCode<=16||keyCode==56||keyCode==67){
                return false;
            }
            else {
                Toast.makeText(getApplicationContext(),"error:只能输入数字和字符'.'",Toast.LENGTH_SHORT).show();
                return true;
            }
        }
    });

    //设置weight监听器
    weight.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode>=7&&keyCode<=16||keyCode==56||keyCode==67){
                return false;
            }
            else {
                Toast.makeText(getApplicationContext(),"Error: 只能输入数字或者字符'.'",Toast.LENGTH_SHORT).show();
                return true;
            }
        }
    });
}

附:完整代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="基本信息"
            android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/NameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <EditText
            android:id="@+id/name"
            android:layout_width="300dp"
            android:layout_height="wrap_content"

            android:text="" />
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高:"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <EditText
            android:id="@+id/height"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="190" />
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CM"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="体重:"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <EditText
            android:id="@+id/weight"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="130"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1"-->
        android:layout_weight="0.72" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="KG"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!---->
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
        <RadioGroup
            android:id="@+id/sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/sex_boy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男" />

            <RadioButton
                android:id="@+id/sex_girl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />
            <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
        </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好:"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <CheckBox
            android:id="@+id/Checkbox_travel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旅游"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
        <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--/>-->
        <CheckBox
            android:id="@+id/Checkbox_sport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="运动"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
        <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--/>-->
        <CheckBox
            android:id="@+id/Checkbox_other"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其他"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
        <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--/>-->

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/major"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="专业:"/>
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </Spinner>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button2"
            android:layout_width="175dp"
            android:layout_height="wrap_content"
            android:onClick="exit"
            android:text="关闭" />
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="add"
            android:text="添加" />
        <!--android:textAppearance="@style/TextAppearance.AppCompat.Display1" />-->
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </ListView>
    </LinearLayout>
</LinearLayout>
package com.example.wangz.experimentation_1;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Checksum;
import com.example.wangz.experimentation_1.Student;

public class experi_1 extends AppCompatActivity {
//  声明需要用到的参数8
    Button adding;
    List<Student> students = new ArrayList<Student>();
    List<String> information = new ArrayList<String>();
    List<String> list = new ArrayList<String>();
    String hobby = "";
    String sex= "";
    String major = "";
    String spinner_id;
    List<String>data_list = new ArrayList<String>();
    Spinner spinner;
    ListView listView;
    ArrayAdapter<String> arr_adapter2;
    ArrayAdapter<String> arr_adapter1;
    EditText name;
    CheckBox check1;
    CheckBox check2;
    CheckBox check3;
    EditText height;
    EditText weight;
    RadioButton boy;
    RadioButton girl;
    public int num=3;

//  menu设置
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.caidan,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.check:
                Toast.makeText(this,"csajci",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }


    //spinner 和 listview 适配器
    //为事件设置监听器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page);
        spinner = (Spinner)findViewById(R.id.spinner);
        listView = (ListView)findViewById(R.id.listview);
        adding = (Button)findViewById(R.id.button1) ;
        name =   findViewById(R.id.name);
        check1 = findViewById(R.id.Checkbox_travel);
        check2 = findViewById(R.id.Checkbox_sport);
        check3 = findViewById(R.id.Checkbox_other);
        height = findViewById(R.id.height);
        weight = findViewById(R.id.weight);
        boy = findViewById(R.id.sex_boy);
        girl = findViewById(R.id.sex_girl);

        //listview数据
        list.add("赵明");
        students.add(new Student("赵明","","","","",""));
        list.add("李晓");
        students.add(new Student("李晓","","","","",""));
        list.add("王丽");
        students.add(new Student("王丽","","","","",""));

        //spinner的数据源
        data_list.add("计算机");
        data_list.add("软件工程");
        data_list.add("物联网");

        //适配器
        arr_adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,data_list);
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arr_adapter1);
        arr_adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_list_item_1);
        listView.setAdapter(arr_adapter2);

        //为listview设置长按操作
        listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("选择操作");
                menu.add(0,0,0,"查看");
                menu.add(0,1,0,"删除");
            }
        });


        //添加按钮点击事件 并判断姓名栏是否为空
        adding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(name.getText()==null||name.getText().length()==0) {
                    Toast.makeText(getApplicationContext(),"姓名为空!", Toast.LENGTH_SHORT).show();
                }
                else {
//                    Toast.makeText(getApplicationContext(),name.getText(),Toast.LENGTH_SHORT).show();
                      if(check1.isChecked()){
                          hobby+="旅游  ";
                      }
                      if(check2.isChecked()){
                          hobby+="运动  ";
                      }
                      if(check3.isChecked()){
                          hobby+="其他  ";
                      }
                      if(boy.isChecked()){
                          sex+="男";
                      }
                      else{
                          sex +="女";
                      }
                      onItemSelected();
                      list.add(name.getText().toString());
                      listView.setAdapter(arr_adapter2);
                      students.add(new Student(name.getText().toString(),height.getText().toString(),weight.getText().toString(),sex,hobby,spinner_id));
                      //Toast的第一种用法
                      //静态方法MakeText,第一个参数是 context, 就是活动的上下文,
                      // 第二个参数是 Toast 显示的文本内容, 第三个参数是显示的时长,有 Toast.LENGTH_LONG 和  Toast.LENGTH_SHORT
                      Toast.makeText(getApplicationContext(),students.get(num).speak(),Toast.LENGTH_SHORT).show();
                      num++;
                      sex="";
                      hobby="";
                }
            }
        });

        //设置heght监听器 通过键盘按键事件
        height.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode>=7&&keyCode<=16||keyCode==56||keyCode==67){
                    return false;
                }
                else {
                    Toast.makeText(getApplicationContext(),"error:只能输入数字和字符'.'",Toast.LENGTH_SHORT).show();
                    return true;
                }
            }
        });

        //设置weight监听器
        weight.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode>=7&&keyCode<=16||keyCode==56||keyCode==67){
                    return false;
                }
                else {
                    Toast.makeText(getApplicationContext(),"Error: 只能输入数字或者字符'.'",Toast.LENGTH_SHORT).show();
                    return true;
                }
            }
        });
    }


    public boolean onContextItemSelected(MenuItem item){
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        String id = String.valueOf(info.id);
        int choice = item.getItemId();
        if (choice==0) {

//                Toast.makeText(this, students.get(Integer.parseInt(id)).speak(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this,see_info.class);
            intent.putExtra("name",students.get(Integer.parseInt(id)).getName());
            intent.putExtra("height",students.get(Integer.parseInt(id)).getHeight());
            intent.putExtra("weight",students.get(Integer.parseInt(id)).getWeight());
            intent.putExtra("sex",students.get(Integer.parseInt(id)).getSex());
            intent.putExtra("hobby",students.get(Integer.parseInt(id)).getHobby());
            intent.putExtra("major",students.get(Integer.parseInt(id)).getMajor());
            startActivity(intent);
            return true;

        }
        else {
            list.remove(Integer.parseInt(id));
            students.remove(Integer.parseInt(id));
            listView.setAdapter(arr_adapter2);
            return super.onContextItemSelected(item);
        }
    }
    public void onItemSelected() {
        spinner_id = spinner.getSelectedItem().toString();
    }

    public void exit(View view){
        System.exit(0);
    }

}

package com.example.wangz.experimentation_1;

import java.io.Serializable;

public class Student implements Serializable {
    String name;
    String height;
    String weight;
    String sex;
    String hobby;
    String major;
    Student(String name,String height,String weight,String sex,String hobby,String major){
        this.name = name;
        this.height = height;
        this.weight = weight;
        this.sex = sex;
        this.hobby = hobby;
        this.major = major;
    }
    Student(){
        super();
    }
    public void setname(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setheight(String height){
        this.height = height;
    }
    public String getHeight(){
        return height;
    }
    public void setweight(String weight){
        this.weight = weight;
    }
    public String getWeight(){
        return weight;
    }
    public void setsex(String sex){
        this.sex = sex;
    }
    public String getSex(){
        return sex;
    }
    public void sethooby(String hobby){
        this.hobby = hobby;
    }
    public String getHobby(){
        return hobby;
    }
    public void setmajor(String major){
        this.major = major;
    }
    public String getMajor(){
        return major;
    }
    
    public String speak(){
        String info = "姓名:"+name+'\n'
                     +"身高:"+height+'\n'
                     +"体重:"+weight+'\n'
                     +"性别:"+sex+'\n'
                     +"爱好:"+hobby+'\n'
                     +"专业:"+major;
        return info;
    }
}

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值