Android学习--界面设计

1、创建工程

创建一个Android工程

Android学习--界面设计_第1张图片

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(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(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;
            }
        }
    });
}

附:完整代码




    

        
    

    

        
        

        
        
    
    

        
        

        
        

        
        
    
    

        
        

        
        
        android:layout_weight="0.72" />

        
        
    
    

        
        

        

        
        
        
        
        
        

            

            
            
        
    
    

        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

    

    

        
        
        
        
    

    

        
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 students = new ArrayList();
    List information = new ArrayList();
    List list = new ArrayList();
    String hobby = "";
    String sex= "";
    String major = "";
    String spinner_id;
    Listdata_list = new ArrayList();
    Spinner spinner;
    ListView listView;
    ArrayAdapter arr_adapter2;
    ArrayAdapter 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(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(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;
    }
}

 

你可能感兴趣的:(android)