Android 开发:(三)常用控件及实践篇

一、常用控件:
1、文本类控件

TextView 负责展示文本,非编辑
EditText 可编辑文本控件

2、按钮类控件

Button 按钮
ImageButton 图片按钮
RadioButton与RadioGroup 单选按钮
CheckBox 复选按钮

3、图片控件

ImageView 负责显示图片

控件详解可以参考:http://blog.csdn.net/netdxy/article/details/50691915

二、登录界面实现:

1.效果图:

2.代码示例:
activity_login.xml:

 
        
        
    

    
        
        
    

/*RadioGroup搭配RadioButton
	RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听
*/
      //横向布局

        

        

    

    

LoginActivity:

public class LoginActivity extends AppCompatActivity {

    private Button loginBtn;
    private EditText userName;
    private EditText passWord;
    private RadioGroup radioGroup;
    private RadioButton radioButton;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        userName = (EditText) findViewById(R.id.username);
        passWord = (EditText) findViewById(R.id.password);

        //给radioGroup添加点击事件
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
		            //通过getCheckedRadioButtonId()获取点击的radiobutton
                int rgId = radioGroup.getCheckedRadioButtonId();
                radioButton = (RadioButton) findViewById(rgId);
            }
        });
        //点击登录
        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
			            //通过getText()获取控件值
                String usernameStr = userName.getText().toString();
                String passwordStr = passWord.getText().toString();
                String loginTypeStr = radioButton.getText().toString();
                
                if (loginTypeStr.equals("个人登录")) {
	                //alert提示框
                    Dialog alertDialog = new AlertDialog.Builder(LoginActivity.this).
                            setTitle("登录状态").
                            setMessage("成功!").
                            create();
                    alertDialog.show();
                }else {
                    Dialog alertDialog = new AlertDialog.Builder(LoginActivity.this).
                            setTitle("登录状态").
                            setMessage("失败!").
                            create();
                    alertDialog.show();
                }
		}

你可能感兴趣的:(Android,入门篇,Android,学习之路,-,入门篇)