You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 KiB

线性布局案例文档 布局界面MainActivity.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="用户登录"
    android:textSize="40sp"
    android:textColor="#000000"
    android:textStyle="bold"
    android:layout_marginTop="100dp"
    android:layout_marginBottom="100dp"
    android:layout_gravity="center"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:"
        android:textSize="30sp"
        android:textColor="#000000"   />
    <EditText
        android:id="@+id/edit_inputname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:textSize="30sp"     />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密    码:"
        android:textSize="30sp"
        android:textColor="#000000"  />
    <EditText
        android:id="@+id/edit_inputpwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:textSize="30sp"     />
</LinearLayout>

<CheckBox
    android:id="@+id/check_remeber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="记住密码"
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_gravity="right"
    android:layout_margin="10dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="center">
    <Button
        android:id="@+id/button_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消"
        android:textSize="30sp"
        android:textColor="#050505"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/button_yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="30sp"
        android:textColor="#050505"
        android:layout_weight="1"     />
</LinearLayout>

帧布局案例文档
布局界面MainActivity.xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/myinfo_login_bg"     >
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/default_icon"
        android:layout_gravity="center"
        android:layout_marginTop="-20dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="admin"
        android:textSize="30sp"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"/>
</FrameLayout>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="个人中心"
    android:textSize="30sp"
    android:textStyle="bold"
    android:layout_gravity="center"
    android:layout_marginBottom="20dp"/>
<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#E91A1A"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置"
    android:textSize="25sp"
    android:padding="10dp"        />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#C5C2C2"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="修改密码"
    android:textSize="25sp"
    android:padding="10dp"        />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#C5C2C2"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="作品详情"
    android:textSize="25sp"
    android:padding="10dp"        />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#C5C2C2"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="注销"
    android:textSize="25sp"
    android:padding="10dp"        />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#C5C2C2"/>

Dialog对话框 布局界面activity_main.xml

<Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="普通对话框"
    android:textAllCaps="false"
    android:textSize="28sp" />

<Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="单选对话框"
    android:textAllCaps="false"
    android:textSize="28sp" />

类文件MainActivity.java package com.example.androidalertdialog;

import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button;

//【任务1】 创建一个普通AlertDialog //【任务2】 创建一个单选AlertDialog public class MainActivity extends AppCompatActivity { //1.定义对象 Button btn_1; Button btn_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.绑定控件 btn_1=findViewById(R.id.button_1); btn_2=findViewById(R.id.button_2);

    //3.按钮单击事件
    /*****************************按钮1*****************************/
    btn_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //1.创建一个AlertDialog的构造器
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            //2.用构造器构建AlertDialog的每一个组成部分
            builder.setIcon(R.mipmap.ic_launcher_round);    //设置图标
            builder.setTitle("温馨提示");     //设置标题
            builder.setMessage("确定要退出吗?");        //设置提示内容
//设置确定按钮
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                      @Override
                public void onClick(DialogInterface dialog, int which) {
                    //添加确定按钮后的代码
                    finish(); //程序页面关闭
                }
            });
//设置取消按钮
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {                      @Override
                public void onClick(DialogInterface dialog, int which) {
                    //添加取消按钮后的代码
                }
            });
            //3.组合AlertDialog的各部分并显示
            builder.create().show();
        }
    });

     /*****************************按钮2*****************************/
    btn_2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //1.创建一个AlertDialog的构造器
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            //2.用构造器构建AlertDialog的每一个组成部分
            builder.setIcon(R.mipmap.ic_launcher_round);    //设置图标
            builder.setTitle("请选择城市");     //设置标题
            final String[] cities={"北京", "上海", "广州", "深圳"}; //设置选项内容
            builder.setSingleChoiceItems(cities, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
//设置确定按钮
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                     @Override
                public void onClick(DialogInterface dialog, int which) {
                    //添加确定按钮后的代码
                }
            });
            //设置取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //添加取消按钮后的代码
                }
            });
            //3.组合AlertDialog的各部分并显示
            builder.create().show();
        }
    });
}

}

Spinner下拉列表框
布局界面activity_main.xml

<Spinner
    android:id="@+id/spinner_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
类文件MainActivity.java package com.example.androidspinner; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { //1.定义对象 Spinner spin1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.绑定控件 spin1=findViewById(R.id.spinner_1); //3.设置控件Spinner的下拉列表项 String[] arr1={"全部","纪录片","漫画","音乐","舞蹈","游戏"}; //4.使用适配器(连接控件与数据的桥梁),告诉控件以什么样的方式显示数据 ArrayAdapter adapter1=new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,arr1); //5.让控件Spinner显示数据 spin1.setAdapter(adapter1); //6.添加选择下拉选项后触发的事件 spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { //选项被选中时执行的方法 //arg0:Spinner控件 arg1选中的列表项 arg2选中项的位置 arg3:选中项ID TextView txt1=(TextView)arg1;//定义选中项 String str1=txt1.getText().toString();//获取选中项的文本内容 Toast.makeText(MainActivity.this,str1,Toast.LENGTH_LONG).show();//用toast将文本显示出来 }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //选项未被选中时执行的方法
        }
    });
}

}

SharedPreferences存储 界面布局activity_main.xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:src="@mipmap/ic_launcher_round"
    android:layout_marginTop="150dp"
    android:layout_marginBottom="50dp"
    android:layout_gravity="center"/>
<EditText
    android:id="@+id/editText_inputname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入用户名"
    android:textColor="#000000"
    android:textSize="25sp"
    android:textStyle="bold"/>
<EditText
    android:id="@+id/editText_inputpwd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码"
    android:inputType="textPassword"
    android:textColor="#000000"
    android:textSize="25sp"
    android:textStyle="bold"/>
<CheckBox
    android:id="@+id/checkBox_reme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="记住密码"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"
    android:textColor="#000000"
    android:textSize="20sp"
    android:textStyle="bold"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"/>
<Button
    android:id="@+id/button_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="登录"
    android:textColor="#000000"
    android:textSize="25sp"
    android:textStyle="bold"/>
4 类文件MainActivity.java package com.example.zsshardpreferenct;

import android.content.Intent; import android.content.SharedPreferences; 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;

public class MainActivity extends AppCompatActivity { //第一步:定义对象 EditText edit_name,edit_pwd; CheckBox check_reme; Button btn_login;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //第二步:绑定控件
    edit_name=findViewById(R.id.editText_inputname);
    edit_pwd=findViewById(R.id.editText_inputpwd);
    check_reme=findViewById(R.id.checkBox_reme);
    btn_login=findViewById(R.id.button_login);

    //第三步:按钮单击事件
    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //单击按钮将输入的用户名、密码、复选框的状态保存起来
     SharedPreferences.Editor editor=getSharedPreferences("myfile",0).edit();
     editor.putString("name", edit_name.getText().toString());
     editor.putString("pwd",edit_pwd.getText().toString());
     editor.putBoolean("st",check_reme.isChecked());
     editor.commit();

            Intent intent=new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
            finish();

        }
    });

    //第四步:如果选中了记住密码复选框,下一次启动,能够获取用户名和密码并显示出来
    String myname=getSharedPreferences("myfile",0).getString("name","");
    String mypwd=getSharedPreferences("myfile",0).getString("pwd","");
    Boolean myst=getSharedPreferences("myfile",0).getBoolean("st",false);
   if(myst==true){
       edit_name.setText(myname);
       edit_pwd.setText(mypwd);
       check_reme.setChecked(true);
   }else{
       edit_name.setText("");
       edit_pwd.setText("");
       check_reme.setChecked(false);
   }

}

}