android练习

package com.example.wang.testapp2;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class ZuoyeActivity extends AppCompatActivity {

    Button bt_2;
    Button bt_3;

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

        bt_2=(Button)findViewById(R.id.bt_2);
        bt_3=(Button)findViewById(R.id.bt_3);
    }

    //
    public void bt1_OnClick(View v)
    {
        String string="";

        Random random = new Random();

        final String[] str =new String[6];

        for(int i=0;i<6;i++)
        {
            int t=random.nextInt(9);

            str[i]=String.valueOf(t);

            string=string+str[i];

        }
        if (str[0]=="0")
        {
            str[0]=String.valueOf(random.nextInt(9));

            string=str[0]+str[1]+str[2]+str[3]+str[4]+str[5];
        }

        Log.e("TAG", "string=" + string);

        View view=View.inflate(this,R.layout.activity_zuoye2,null);

      final AlertDialog alertDialog = new AlertDialog.Builder(this)

                .setTitle("生成最大值和最小值")
                .setView(view)
                .setPositiveButton("最小值", new DialogInterface.OnClickListener() {

                    String string1="";
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        AlertDialog al =(AlertDialog)dialog;

                        int[] a = new int[6];
                        for (int i = 0; i < 6; i++) {
                            a[i] = Integer.parseInt(str[i]);
                            Log.e("TAG","a[}="+a[i]);
                        }

                        for (int i = 0; i < 6; i++) {
                            for (int j = 0; j < 5-i; j++) {
                                if (a[j] > a[j + 1]) {
                                    int b = a[j];

                                    a[j] = a[j + 1];
                                    a[j + 1] = b;
                                }
                            }
                        }
                        for (int i = 0; i < 6; i++) {

                            str[i]=String.valueOf(a[i]);

                            string1=string1+str[i];
                        }

                        Intent intent=new Intent(ZuoyeActivity.this,ZuoyeActivity3.class);

                        //intent.setData(Uri.parse(string1));

                        intent.putExtra("jieguo",string1);

                        startActivity(intent);

//                       EditText et_jieguo=(EditText)al.findViewById(R.id.et_jieguo);
//
//                        et_jieguo.setText(string1);

                    }

                })
                .setNegativeButton("最大值", null)
                .show();

        EditText et_suiji=(EditText)alertDialog.findViewById(R.id.et_suiji);

        et_suiji.setText(string);

    }

    public void bt2_OnClick(View v)
    {
        final AlertDialog alertDialog=new AlertDialog.Builder(this)

                .setTitle("提示")
                .setMessage("确定要删除吗?" + "\n" + "要删除,请点击“是”。")
                .setNegativeButton("否", null)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(ZuoyeActivity.this, "删除成功" + which, Toast.LENGTH_SHORT).show();

                    }
                })
                .show();
    }

    public void bt3_OnClick(View v)
    {

         View  view=View.inflate(this, R.layout.activity_zuoye1, null);

        new AlertDialog.Builder(this)
                .setTitle("请输入电话号码")
                .setView(view)
                .setNeutralButton("拨出此号码", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        AlertDialog al = (AlertDialog) dialog;

                        EditText et_1 = (EditText) al.findViewById(R.id.et_1);

                        String str = et_1.getText().toString();

                        Intent intent = new Intent(Intent.ACTION_CALL);

                        intent.setData(Uri.parse("tel:" + str));

                        try {
                            startActivity(intent);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                })
                .setPositiveButton("向此号码发短信", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        AlertDialog al=(AlertDialog)dialog;

                        EditText et_1 = (EditText) al.findViewById(R.id.et_1);

                        String str = et_1.getText().toString();

                        Intent intent = new Intent(Intent.ACTION_SENDTO);

                        intent.setData(Uri.parse("smsto:"+str));

                        startActivity(intent);

                    }
                })
                .show();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wang.testapp2.ZuoyeActivity3">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/et_jieguo" />

</RelativeLayout>

package com.example.wang.testapp2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

public class ZuoyeActivity3 extends AppCompatActivity {

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

        EditText et_jieguo=(EditText)findViewById(R.id.et_jieguo);

        Intent intent=getIntent();

        String s=intent.getStringExtra("jieguo");

        et_jieguo.setText(s);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="练习题1"
        android:id="@+id/bt_1"
        android:onClick="bt1_OnClick"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="练习题2"
        android:id="@+id/bt_2"
        android:onClick="bt2_OnClick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="练习题3"
        android:id="@+id/bt_3"
        android:onClick="bt3_OnClick"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="随机数:"
            android:textSize="25sp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_suiji" />

    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/et_jieguo" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wang.testapp2.ZuoyeActivity3">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/et_jieguo" />

</RelativeLayout>

时间: 2024-07-30 17:55:10

android练习的相关文章

android开发之onCreate( )方法详解

onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢? 先看看Google Android Developers官网上的解释: onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource d

如何用 Android Studio 导入开源项目以及常见错误的解决办法

声明:这篇文章完全来自这篇文章,感谢大神的相助.这篇文章纯粹是为了备份. 本篇以Github上最热门的MaterialDesign库,MaterialDesignLibrary来介绍怎样使用Android Sudio导入开源项目的,如果你和我一样刚刚从Eclipse转到AS,那本篇文章非常适合你. 如果不引入任何第三方库,不做自动化分渠道打包等操作,那可以在完全不了解Gradle的情况下进行Android项目的开发.但如果要想导入Github上的热门项目,必须首先熟悉Gradle. 1. Gra

Android——调用高德地图API前期准备

1.登陆高德开放平台注册账号http://lbs.amap.com/ 2.创建自己的应用并且添加新key 获取发布版安全码获取方法: 在AndroidStudio的Terminal中编译: 输入如下图: 开发版安全码获取: 开发版获取的另一种方法:(和获取发布版本一样在AndroidStudio的Terminal中编译 只是keystore的路径改成debug.keystore    密钥库口令默认是android) 创建key成功 得到key: 前期准备完成 下一篇文章是开发配置 备忘 ,也希

Android小例子:使用反射机制来读取图片制作一个图片浏览器

效果图: 工程文件夹: 该例子可供于新手参考练习,如果有哪里不对的地方,望指正>-< <黑幕下的人> java代码(MainActivity.java): package com.example.imageswitchtest; import java.lang.reflect.Field; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.v

Android Studio 安装及常见问题

今年放假比去年早了一些,就提前回来了.感觉挺爽,结果教研室电脑没有带回来,悲剧是导师让我维护一下以前的项目,只能屁颠屁颠的搞起呀.只能用自己的笔记本搭建android开发环境.由于前阶段听说Android Studio用着爽歪歪,于是尝试在超卡的笔记本上跑Android Studio.哇咔咔,费了九牛二虎之力终于将Android Studio玩转了.先上个图,快过年了,祝大家新年快乐! Android官网也开始推广Android Studio了,以前官网好像提供Eclipse和ADT打包下载,现

Android 导航条效果实现(六) TabLayout+ViewPager+Fragment

TabLayout 一.继承结构 public class TabLayout extends HorizontalScrollView java.lang.Object ? android.view.View ? android.view.ViewGroup ? android.widget.FrameLayout ? android.widget.HorizontalScrollView ? android.support.design.widget.TabLayout 二.TabLayou

Android Studio 连接真机不识别

本人也是初学..写错的请大神多多批评指正! 不胜荣幸!! 强烈推荐使用真机测试..除非是最后关头要测试各个Android系统版本.. 本人遇到的连不上的原因有以下几种: 1  --   手机设置问题.开USB调试   方法:  手机设置 - 开发人员选项 - USB调试  - 勾选 2  --   数据线问题.  有的数据线只能用来充电,有的可以连接存储.识别方法很简单..插上机器有USB存储设备的提示的就可以用.另外数据线如果都露线皮了..就赶紧扔了.十块八块的总比你为这个破问题纠结一下午的好

android Activity 的生命周期 以及横屏竖屏切换时 Activity 的状态变化

生命周期Android 系统在Activity 生命周期中加入一些钩子,我们可以在这些系统预留的钩子中做一些事情.例举了 7 个常用的钩子:protected void onCreate(Bundle savedInstanceState)protected void onStart()protected void onResume()protected void onPause()protected void onStop()protected void onRestart()protecte

谈谈-Android中的接口回调技术

Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James对Android接口回调技术的理解(结合前人的知识和自己的实践). 使用一个比喻很形象地说明:客户端有个疑问打电话请教服务端,但服务端无法现场给出解答,相互之间约定:服务端一旦有答案,使用电话的方式反馈给客户端. 以上有三个主体:客户端.服务端和接口(方式). 接口回调的原理框图说明: Demo界面

修改Android Studio默认的API Level(SDK版本)

原文:修改Android Studio默认的API Level(SDK版本) Android Studio(2.1.2)新建工程的时候只会让你选择最低支持的SDK版本,默认的目标编译SDK版本会以系统当前SDK中最新SDK platform作为目标的API Level.但是很多时候我们并不需要最新的SDK版本,如何修改呢? 方法是:修改工程目录中的Gradle Scripts->build.gradle(Module:app)中的相关行,具体见下图: 当然也可以图形化操作,右键工程目录选择"