版权声明:转载请注明出处:http://blog.csdn.net/kevindongkun https://blog.csdn.net/Kevindongkun/article/details/60746412
一、常用控件:
1、文本类控件
TextView 负责展示文本,非编辑
EditText 可编辑文本控件
2、按钮类控件
Button 按钮
ImageButton 图片按钮
RadioButton与RadioGroup 单选按钮
CheckBox 复选按钮
3、图片控件
ImageView 负责显示图片
控件详解可以参考:http://blog.csdn.net/netdxy/article/details/50691915
二、登录界面实现:
1.效果图:
2.代码示例:
activity_login.xml:
<LinearLayout
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:background="#000000" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/username"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:hint="请输入用户名" />
</LinearLayout>
<LinearLayout
android:id="@+id/passWord"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/userName">
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:background="#000000" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/password"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:password="true"
android:hint="请输入密码"
/>
</LinearLayout>
/*RadioGroup搭配RadioButton
RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听
*/
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/passWord" //相对布局(在xxx下面开始)
android:orientation="horizontal"> //横向布局
<RadioButton
android:id="@+id/personal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_weight="1" //平分空间大小
android:checked="true" //默认选中
android:text="个人登录"/>
<RadioButton
android:id="@+id/legal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="法人登录"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="45dp" //长宽大小:dp
android:layout_marginTop="10dp"
android:layout_below="@id/rg"
android:id="@+id/loginBtn"
android:text="登录"
android:background="@drawable/shape" //button圆角设置,具体步骤见下节。
android:textSize="19sp" //字体大小:sp
android:textColor="#ffffff" />
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();
}
}