Android学习笔记之TextView(文本框)、EditText(编辑框)、RadioButton(单选按钮)、spinner下拉框、CheckBox(复选框)以及Button(按钮)

https://blog.csdn.net/qq_38735475/article/details/85331299

这个博客的文章不错,记录下来,以备需要时认真学习。

 

android学习笔记
近期学习了android,今天下午考完试,分享一下我的android学习笔记吧!还是新手,不足之处请多指教:)
首先,是UI界面,会用到TextView(文本框)、EditText(编辑框)、RadioButton(单选按钮)、spinner下拉框、CheckBox(复选框)以及Button(按钮)等控件。

 

在res->layout->activity_main.xml中写UI界面:

//采用线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="@string/title"      //用Strin变量修改方便,也可用字符串
        android:layout_gravity="center" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/xuehao"/>
        <EditText
            android:id="@+id/sno"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"/>
    </LinearLayout>
    
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/xingming"/>
        <EditText
            android:id="@+id/sname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"/> 
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/xingbie"/>
        //单选按钮
        <RadioGroup
            android:id="@+id/sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"  //根据所占权重分配在该线性布局宽度
                android:text="@string/nan"/>
            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/nv"/>
        </RadioGroup>
        </LinearLayout>
        
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/zhuanye"/>
            //下拉框(还需再strings.xml中添加item标签,随后补上)
        <Spinner
            android:id="@+id/major"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/xuanke"/>
         //复选框
        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/check1"
            />
        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/check2"
            />
        <CheckBox
            android:id="@+id/checkbox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/check3"
            />
    </LinearLayout>
    
    <Button
       android:id="@+id/register"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="@string/zhuce"/>
    //ListView(还需写个Listview中的布局)
    <ListView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/st"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

values下String.xml中添加spinner的Item

 <string-array name="major">
        <item>通信工程</item>
        <item>网络工程</item>
        <item>自动化</item>
    </string-array>

ListView的布局info.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="horizontal" >
    <TextView android:id="@+id/no"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
	<TextView android:id="@+id/name"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
	<TextView android:id="@+id/gender"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
	<TextView android:id="@+id/stmajor"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
	<TextView android:id="@+id/courses"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

 

在src目录下写java文件MainActivity.java实现功能

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String TAG="SpinnerActivity";
	private TextView sno, sname;
	private RadioGroup sex;
	private List<CheckBox> course=new ArrayList<CheckBox>();
	private Spinner major;
	private String itemContent;
	private Button register;
	private ListView info;
	private RadioButton gender;
	final List<Map<String,Object>> infos =new ArrayList<Map<String,Object>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sno=(TextView)findViewById(R.id.sno);
		sname=(TextView)findViewById(R.id.sname);
		sex=(RadioGroup)findViewById(R.id.sex);
		course.add((CheckBox)findViewById(R.id.checkbox1));
		course.add((CheckBox)findViewById(R.id.checkbox2));
		course.add((CheckBox)findViewById(R.id.checkbox3));
		info=(ListView)findViewById(R.id.info);
		register=(Button)findViewById(R.id.register);
		major=(Spinner)findViewById(R.id.major);
		
		//下拉列表
		ArrayAdapter<CharSequence> majorAdapter=ArrayAdapter.createFromResource(this, R.array.major, android.R.layout.select_dialog_multichoice);
		majorAdapter.setDropDownViewResource(android.R.layout.select_dialog_multichoice);
		
		major.setAdapter(majorAdapter);
		major.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int position, long id) {
				Spinner spinner=(Spinner)adapterView;
				 itemContent=(String)adapterView.getItemAtPosition(position);
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> view) {
				Log.i(TAG, view.getClass().getName());
			}
		});
		//取出性别的值
		sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup sex, int checkedId) {
				gender=(RadioButton)findViewById(checkedId);	
			}
		});
				
		//按钮监听器
		register.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				//选课的值
				List<String>values=new ArrayList<String>();
				for(CheckBox cb:course){
					if(cb.isChecked()){
						values.add(cb.getText().toString());
					}
				
				}
				
				//listview(Listview中需要适配器)
				Map<String,Object> info1= new HashMap<String , Object>();
				info1.put("sno",sno.getText());
				info1.put("sname", sname.getText());
				info1.put("gender", gender.getText());
				info1.put("major", major.getSelectedItem().toString());
				info1.put("courses", values);
				infos.add(info1);
				SimpleAdapter infoAdapter=new SimpleAdapter(MainActivity.this, infos, R.layout.info, new String[]{"sno","sname","gender","major","courses"}, new int[]{R.id.no,R.id.name,R.id.gender,R.id.stmajor,R.id.courses});
				info.setAdapter(infoAdapter);
			}
		});
		
			}
}

展示一下运行结果

https://blog.csdn.net/qq_38735475/article/details/85331299

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值