androd备考 平时实验

笑着哭i 提交于 2019-12-23 12:46:37

目录

实验二

注意点:

实现

麻烦但实时的写法

简易写法:(可以不选)

小结:


 

 

实验二

注意点:

滚动布局

ScrollView

滚动布局

新建的模板:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
    

</ScrollView>

加上几行添加周围空白

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        
        >
    </LinearLayout>
    

</ScrollView>

 

注意:hobbys=hobbys.replaceAll(hobby, "");   //一定要重新赋值回给hobbys 该api不会修改原来的hobbys

StringBuffer 的append在此处比string好用多了  还会自动转换

实现

麻烦但实时的写法

activity_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名" />

        <EditText
            android:id="@+id/et_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"
            android:hint="请输入口令" />

        <TextView
            android:id="@+id/tv_major"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="专业"
            android:textSize="30sp" />

        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="软件" />

            <RadioButton
                android:id="@+id/rb2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="物联网" />

            <RadioButton
                android:id="@+id/rb3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="计算机" />

            <RadioButton
                android:id="@+id/rb4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="信管" />
        </RadioGroup>

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="爱好"
            android:textSize="30sp" />

        <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="上网" />
		
        
        <CheckBox
            android:id="@+id/cb_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="运动" />

        <CheckBox
            android:id="@+id/cb_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="读书" />

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="出生年月"
            android:textSize="30sp" />

        <DatePicker
            android:id="@+id/dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:calendarViewShown="false"
            android:maxDate="12-31-2940"
            android:minDate="1-1-1970"
            android:spinnersShown="true" />

        <Button
            android:id="@+id/dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="提交" />
        
    </LinearLayout>

</ScrollView>

MainActivity.java

package com.example.glkexp02;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener,android.widget.RadioGroup.OnCheckedChangeListener{

    private EditText et_name;
	private EditText et_pwd;
	private RadioGroup rg;
	private CheckBox cb1;
	private CheckBox cb2;
	private CheckBox cb3;
	private DatePicker dp;

	private String major="";
	private String hobbys="";
	private String birth="";

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        et_name = (EditText) findViewById(R.id.et_name);
    	et_pwd = (EditText) findViewById(R.id.et_pwd);
    	rg = (RadioGroup) findViewById(R.id.rg);
    	cb1 = (CheckBox) findViewById(R.id.cb_1);
    	cb2 = (CheckBox) findViewById(R.id.cb_2);
    	cb3 = (CheckBox) findViewById(R.id.cb_3);
    	dp = (DatePicker) findViewById(R.id.dp);
    	
    	rg.setOnCheckedChangeListener(this);
    	cb1.setOnCheckedChangeListener(this);
    	cb2.setOnCheckedChangeListener(this);
    	cb3.setOnCheckedChangeListener(this);
    }
	
    public void click(View v){
    	String name = et_name.getText().toString();
    	String pwd = et_pwd.getText().toString();
    	
    	if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
    		Toast.makeText(this, "用户名 或 密码 不能为空!", 0).show();
    		return;
    	}
    	if(TextUtils.isEmpty(major)){
    		Toast.makeText(this, "请选择专业!", 0).show();
    		return;
    	}
    	if(TextUtils.isEmpty(hobbys)){
    		Toast.makeText(this, "请选择爱好!", 0).show();
    		return;
    	}
    	
    	birth=dp.getYear()+" "+(dp.getMonth()+1)+" "+dp.getDayOfMonth();
    	String info="\n用  户 名:"+name+
    				"\n密       码:"+pwd+
    				"\n专       业:"+major+
    				"\n爱       好:"+hobbys+
    				"\n出生年月:"+birth
    			;
    	
    	Toast.makeText(this, info, 1).show();
    }

    //选择专业
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		RadioButton rb_check = (RadioButton) findViewById(checkedId);
		major=rb_check.getText().toString();
		
	}

	//选择爱好
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		String hobby = " "+buttonView.getText().toString();
		if(isChecked){
			hobbys+=hobby;
		}else{
			hobbys=hobbys.replaceAll(hobby, "");//删除此hobby  一定记得赋值回给hobbys
			//Toast.makeText(this, "删除"+hobby+"\n删除后:"+hobbys, 1).show();
		}
	}
}

简易写法:(可以不选)

package com.example.glkexp02;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity{
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
	
    public void click(View v){
    	
    	EditText et_name = (EditText) findViewById(R.id.et_name);
    	EditText et_pwd = (EditText) findViewById(R.id.et_pwd);
    	RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
    	RadioButton rd_check = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
    	CheckBox cb1 = (CheckBox) findViewById(R.id.cb_1);
    	CheckBox cb2 = (CheckBox) findViewById(R.id.cb_2);
    	CheckBox cb3 = (CheckBox) findViewById(R.id.cb_3);
    	DatePicker dp = (DatePicker) findViewById(R.id.dp);
    	
    	StringBuffer info=new StringBuffer();
    	info.append("\n姓        名:"+et_name.getText());
    	info.append("\n密        码:"+et_pwd.getText());
    	if(rd_check!=null) info.append("\n专        业:"+rd_check.getText());
    	info.append("\n爱        好:");
    	if(cb1.isChecked()) info.append(" "+cb1.getText());
    	if(cb2.isChecked()) info.append(" "+cb2.getText());
    	if(cb3.isChecked()) info.append(" "+cb3.getText());
    	info.append("\n出生日期:"+dp.getYear()+"年"+(dp.getMonth()+1)+"月"+dp.getDayOfMonth()+"日");
    	
    	Toast.makeText(this, info+"\n", 1).show();
    }
}

 

 

小结:

界面布局时采用拖拽+修改的方式最为快速,建议采用

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!