1.创建登录页面,要求用户输入账号123、密码456,提示登录成功、否则提示登录失败,登录按钮需要进行默认黄色,点击时显示为粉色,并要求按钮为圆角
登录页面代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="15dp"
android:gravity="center"
android:background="@drawable/tp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="账号:" />
<EditText
android:id="@+id/et_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入账号"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密码:" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:id="@+id/but_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:background="@drawable/but_login_back"/>
<TextView
android:id="@+id/tv_registered"
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户注册" />
</LinearLayout>
Java代码:
private Button but_login;
private EditText et_user;
private EditText et_pwd;
but_login=findViewById(R.id.but_login);
et_pwd=findViewById(R.id.et_pwd);
et_user=findViewById(R.id.et_user);
but_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user=et_user.getText().toString();
String pwd=et_pwd.getText().toString();
if ("123".equals(user)&&"456".equals(pwd)){
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
finish();
}else {
Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
});
调整按钮颜色的xml文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/fen"></solid>
<corners android:radius="30dp"></corners>
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp"></corners>
<solid android:color="@color/huang"/>
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/color" android:state_pressed="true"></item>
<item android:drawable="@drawable/color2"></item>
</selector>
2.在登录页面需要有注册超链接,当用户点击注册时,可以弹出对话框进行注册,此处需要用到输入框、单选和复选,注册完成后,将信息提示出来即可
对话框的页面代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<EditText
android:id="@+id/et_reg_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="请输入账号"
android:gravity="center"/>
<EditText
android:id="@+id/et_reg_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="请输入密码"
android:gravity="center"/>
<EditText
android:id="@+id/et_reg_pwd2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="请再次输入密码"
android:gravity="center"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb_woman"
android:layout_marginStart="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<CheckBox
android:id="@+id/cb_agreement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请阅读并同意该协议" />
</LinearLayout>
java方面的代码:
private TextView tv_registered;
tv_registered=findViewById(R.id.tv_registered);
tv_registered.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View view1= getLayoutInflater().inflate(R.layout.registered, null);
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this)
.setTitle("注册")
.setView(view1)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("注册", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
CheckBox cb_agreement=view1.findViewById(R.id.cb_agreement);
if (cb_agreement.isChecked()){
EditText et_reg_user=view1.findViewById(R.id.et_reg_user);
EditText et_reg_pwd=view1.findViewById(R.id.et_reg_pwd);
EditText et_reg_pwd2=view1.findViewById(R.id.et_reg_pwd2);
RadioButton rb_man=view1.findViewById(R.id.rb_man);
RadioButton rb_woman=view1.findViewById(R.id.rb_woman);
String pwd=et_reg_pwd.getText().toString();
String pwd2=et_reg_pwd2.getText().toString();
String user=et_reg_user.getText().toString();
if (pwd.equals(pwd2)){
if (rb_man.isChecked()){
String man=rb_man.getText().toString();
Toast.makeText(MainActivity.this,"账号为:"+user+",密码为:" +
""+pwd+",重复输入的密码为:"+pwd2+",性别为:"+man,+Toast.LENGTH_SHORT).show();
}else {
String woman=rb_woman.getText().toString();
Toast.makeText(MainActivity.this,"账号为:"+user+",密码为:" +
""+pwd+",重复输入的密码为:"+pwd2+",性别为:"+woman,+Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(MainActivity.this,"你两次输入的密码不一致,请重新输入",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(MainActivity.this,"请先同意协议再进行注册",Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
}
});
}
3.当用户登录成功后,进入到内容页面,内容页面上需要进行下拉选择,自行定义内容,比如我可以在下拉框中定义几个明星,当我选中周杰伦时,就将该明星的简介写入TextView中
下拉框页面代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2"
android:orientation="vertical">
<Spinner
android:id="@+id/sp_mingxing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_jianjie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_weight="1" />
</LinearLayout>
java页面代码:
private Spinner sp_mingxing;
private TextView tv_jianjie;
private String[] mingzi={"周杰伦","陈奕迅","林俊杰"};
private String[] jieshao={"111","222","333"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
sp_mingxing=findViewById(R.id.sp_mingxing);
tv_jianjie=findViewById(R.id.tv_jianjie);
tv_jianjie.setText(jieshao[0]);
ArrayAdapter arrayAdapter=new ArrayAdapter(MainActivity2.this, android.R.layout.simple_list_item_1,mingzi);
sp_mingxing.setAdapter(arrayAdapter);
sp_mingxing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
tv_jianjie.setText(jieshao[i]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});