使用表格布局管理器完成附件中图片所示的程序,将密码设置为“123456”,在按下登录按钮后,显示“欢迎XXX”。然后清空姓名和密码的编辑框。如果选择取消则直接清除所有编辑框中的内容。
提示:XXX为输入的姓名。
1. activity_main.xml
2. MainActivity.java
package com.example.bean.myapplication34v2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et1 =(EditText)findViewById(R.id.ed1);
final EditText et2 =(EditText)findViewById(R.id.ed2);
Button bt1 = (Button)findViewById(R.id.bt1);
Button bt2 = (Button)findViewById(R.id.bt2);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et1.setText("请输入姓名");
et2.setText("");
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et2.getText().toString().equals("123456"))
{
Toast toast = Toast.makeText(MainActivity.this,et1.getText().toString()+"登陆成功",Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(MainActivity.this,"密码错误!请重新输入",Toast.LENGTH_LONG);
toast.show();
}
}
});
}
}
3. 结果展示
2. MainActivity.java
package com.example.bean.myapplication34v3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button)findViewById(R.id.button);
final EditText ed1 = (EditText)findViewById(R.id.ed1);
final RadioGroup group = (RadioGroup)findViewById(R.id.group1);
final CheckBox like1 = (CheckBox)findViewById(R.id.like1);
final CheckBox like2 = (CheckBox)findViewById(R.id.like2);
final CheckBox like3 = (CheckBox)findViewById(R.id.like3);
final CheckBox like4 = (CheckBox)findViewById(R.id.like4);
final TextView text = (TextView)findViewById(R.id.text0);
final String str[] = new String[3];
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str[0] = ed1.getText().toString();
for(int i = 0; i<group.getChildCount(); i++){
RadioButton sex = (RadioButton)group.getChildAt(i);
if (sex.isChecked()){
str[1] = (String) sex.getText();
break;
}
}
String like = "";
if (like1.isChecked()){
like+=like1.getText().toString() + "、";
}
if (like2.isChecked()){
like+=like2.getText().toString() + "、";
}
if (like3.isChecked()){
like+=like3.getText().toString() + "、";
}
if (like4.isChecked()){
like+=like4.getText().toString() + "、";
}
if(like.isEmpty()){
}
else {
like = like.substring(0, like.length() - 1);
}
str[2]=like;
text.setText(str[0]+","+ str[1] + ",喜欢:" + str[2]);
}
});
}
}
3. 结果展示
作者:江山点墨