Android集训第四天

上午

ProgressBar和SeekBar,这两个都比较简单,无非就是给用户一个显示信息,程序需要好多时间

Progress经常与线程的东西一起实现


示例:

progressBar = findViewById(R.id.progressBar);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(true){
                progressBar.setProgress(progress);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if((progress + 5) < 100){
                    progress += 5;
                }else{
                    progress = 0;
                }
            }
        }
    }).start();

这里我新建了一个线程,同时改写其run方法,然后在运行start;

SeekBar拖动事件需要改写的方法就比较多

seekBar = findViewById(R.id.seekBar);
seekBar.setMax(100);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        textView.append("Current Progress"+String.valueOf(progress)+"\n");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        textView.append("Start Progress\n");
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        textView.append("Stop Progress\n");
    }
});

先定义SeekBar的最大长度,然后在调用setOnSeekBarChangeListener这个方法实现

设置一个基本对话框,没有按钮

Dialog dialog = new AlertDialog.Builder(this).setTitle("对话框")
            .setMessage("显示提示信息")
            .setIcon(R.drawable.ic_launcher_background)
            .create();
    dialog.show();
}

设置一个对话框,有按钮的

以下是一个示例:

我觉得用代码来理解很好理解

Dialog dialog = new AlertDialog.Builder(MainActivity.this)
    .setTitle("确定要删除吗?")
            .setMessage("您确定要删除这条消息吗?")
            .setIcon(R.drawable.ic_launcher_background)
            .setPositiveButton("确定", new DialogInterface.OnClickListener(){
                      @Override
                      public void onClick(DialogInterface dialog, int which) {

                           }
                }).setNeutralButton("查看详情", new DialogInterface.OnClickListener(){
                        @Override
                           public void onClick(DialogInterface dialog, int which) {       

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

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

Toast显示信息的一种机制

Toast.makeText(MainActivity.this,"hello",Toast.LENGTH_SHORT).show();
下午

Activity
Intent开启第二个界面
首先在java里面新创建一个继承Activity的java文件,新的界面肯定要有新的布局,所以在layout里面新创建一个xml文件,
然后在新的java文件里面写入

protected void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(新的布局的名字);

然后他们就相匹配了(可以这样理解),然后看看AndroidMainifest里面有没有给新的activity注册
如果没有注册,就自己手动添加如下代码

<activity android:name="这里是你的新创建的java文件名字">

    </activity>

这样大致就好了,最后在原来的java文件里面敲出如下代码

Intent it = new Intent(A,B.class);//A,B表示从A界面跳到B界面
    startActivity(it);

当然Intent的功能还不止这些,还可以传递数据

发送方用.putExtra(“key”,”你要发送的消息”);
key(可以这样理解,双方都已知,相当于秘钥,我们都知道就可以互相通信;如果接收方错了,就不能收到)
接收方用Intent it = getIntent();这个方法获取Intent,然后再用这个方法it.getStringExtra(“key”)
得到发送方所发送的东西