ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

安卓第五周

2021-10-07 18:02:52  阅读:184  来源: 互联网

标签:安卓 DialogInterface AlertDialog 第五 dialog import android void


1.返回键实现对话框弹出是否退出应用程序

package com.example.commondialog;

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

import android.content.DialogInterface;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public void onBackPressed() {
        AlertDialog dialog;
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
                .setTitle("普通对话框")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("是否确定退出应用:“")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,int which) {
                        dialog.dismiss();
                        MainActivity.this.finish();

                    }

                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
        dialog=builder.create();
        dialog.show();
    }
}

 

 

2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体

package com.example.myapplication;



import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private int num=0;
    private int num1[]={0,1};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
                .setTitle("性别")
                .setSingleChoiceItems(new String[]{"男","女"}, -1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        num=which;
                    }
                })
                .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        if(num==0){
                            Toast.makeText(MainActivity.this, "你选择的是男", Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(MainActivity.this, "你选择的是女", Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
        AlertDialog dialog=builder.create();
        dialog.show();
    }
}

 

 

3.布局(详见:Android第五周上机word文档)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:background="#31B404"
    android:layout_margin="2dp"
    tools:context=".MyLayout">
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1.TextView显示文本信息"
        android:textSize="20sp"
        android:textColor="#FF0000"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginTop="10dp"
        />
    <Button
        android:id="@+id/btn_1"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:text="2.按钮"
        android:textSize="20sp"
        android:textColor="#FF0000"
        android:textStyle="bold"
        android:padding="5dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tv_1"
        />
    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="3.编辑框:请输入信息"
        android:textSize="20sp"
        android:textColorHint="#FF0000"
        android:layout_below="@id/btn_1"
        android:layout_alignStart="@id/btn_1"
        android:layout_margin="5dp"
        />
    <RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/et_1"
        android:layout_alignLeft="@id/et_1"
        >
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4.男"
            android:textColor="#FF0000"
            android:textSize="20sp"
            android:textStyle="bold"
            />
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textColor="#FF0000"
            android:textSize="20sp"
            android:textStyle="bold"
            />
        </RadioGroup>
    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@id/rg_1"
        android:layout_alignLeft="@id/rg_1">
        <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电脑"
            android:textSize="20sp"
            />
        <CheckBox
            android:id="@+id/cb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机"
            android:textSize="20sp"/>
    </LinearLayout>
</RelativeLayout>

 

 

4.教材p76页 图3—17购物商城界面

<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="购物商城"
    android:textColor="#E8E3E9"
    android:background="#E0A9BA"
    android:textSize="20sp"
    android:gravity="center"/>
<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <TextView
        android:id="@+id/gouqu"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#E91E63"
        android:text="购物商城"
        android:textSize="35sp"
        android:gravity="center"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        android:layout_below="@id/gouqu"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".List_item"
    android:padding="16dp">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="120dp"
        android:layout_height="90dp"
        android:layout_centerVertical="true"/>
    <RelativeLayout
        android:id="@+id/rl_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/iv"
        android:layout_centerVertical="true">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="桌子"
        android:textColor="#000000"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="价格"
        android:textColor="#FF8F03"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_below="@id/title"/>
    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1000"
        android:textSize="20sp"
        android:textColor="#FF8F03"
        android:layout_toRightOf="@id/tv_price"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/title"/>
    </RelativeLayout>
</RelativeLayout>

 

标签:安卓,DialogInterface,AlertDialog,第五,dialog,import,android,void
来源: https://www.cnblogs.com/leopard1/p/15376571.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有