Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

android高级界面

$
0
0

RadioButton与CheckBox

  • RadioGroup 和RadioButton

RadioButton一定要放在RadioGroup
Checked=“true”当按钮选中后
勾选事件监听
OnCheckedChangeListener
练习
石头,剪刀,布
MainActivity.java

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
    TextView tv;
    Button btn_begin;
    RadioButton radiobtn[]=new RadioButton[3];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radiobtn[0] =(RadioButton) findViewById(R.id.radio0);
        radiobtn[1]=(RadioButton) findViewById(R.id.radio1);
        radiobtn[2]=(RadioButton) findViewById(R.id.radio2);
        tv=(TextView) findViewById(R.id.textView1);
        btn_begin=(Button) findViewById(R.id.btn_begin);
        btn_begin.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        //生成随机数
         int random=(int) (Math.random()*10+10);

         new AsyncTask<Integer, Integer, String>() {
             //后台线程
             @Override
                protected String doInBackground(Integer... params) {
                     int num=params[0];
                     int i=0;
                     while(i<num){
                         i++;
                         publishProgress(i%radiobtn.length);
                         try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                     }
                     return radiobtn[i%radiobtn.length].getText().toString();
                }
             protected  void onProgressUpdate(Integer... values) {

                 int count=values[0];
                 radiobtn[count].setChecked(true);
             };

             protected void onPostExecute(String result) {
                 super.onPostExecute(result);
                 tv.setText(result);
             };
        }.execute(random);
    }
}

Activity_main.xml

<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.android_829_radiobutton.MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:textSize="30sp"
        android:text="猜拳" />
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp" >
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="石头" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="剪刀" />
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="布" />
    </RadioGroup>
    <Button
        android:id="@+id/btn_begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:text="开始" />
</RelativeLayout>

菜单Menu

PopupMenu
MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //创建PopupMenu
                PopupMenu pop=new PopupMenu(MainActivity.this, v);
                //解析文件
                getMenuInflater().inflate(R.menu.main, pop.getMenu());
                //显示
                pop.show();
                //监听PopupMenu菜单
                pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int id=item.getItemId();
                        if(id==R.id.item2){
                            finish();
                        }
                        return false;
                    }
                });
            }
        });
    }
}

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.android_829_menu.MainActivity" >
    <item
        android:id="@+id/item0"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="登录"/>
    <item
        android:id="@+id/item1"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="注册"/>
    <item
        android:id="@+id/item2"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="退出"/>
</menu>

activity_main.xml

<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.android_829_menu.MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="137dp"
        android:text="Button" />
</RelativeLayout>

OptionMenu
点击menu按钮,显示菜单

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
 //添加子菜单  
        SubMenu sub1=menu.addSubMenu("设置");  
        sub1.add(1,SET_ITEM1,300,"设置声音");  
      sub1.add(1,SET_ITEM2,400,"设置桌面");  
      SubMenu sub2=menu.addSubMenu("选择");  
      sub2.add(1,SET_ITEM3,300,"选择一");  
      sub2.add(1,SET_ITEM4,400,"选择二");  

        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.item0) {
            return true;
        }else if(id==R.id.item1){
            finish();
            return true;
        }else if(id==R.id.item2){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

ContextMenu
长按控件,显示菜单

TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById(R.id.textView1);
        //注册tv,当用户长按tv时,触发上下文菜单
        registerForContextMenu(tv);
    }
  //创建上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.addSubMenu(0, 1, 0, "收藏");
        menu.addSubMenu(0, 2, 0, "删除");

        Toast.makeText(MainActivity.this, "长按后", Toast.LENGTH_SHORT).show();
    }
   @Override
    public boolean onContextItemSelected(MenuItem item) {
           if(item.getItemId() == 1){
                Toast.makeText(this, "收藏", Toast.LENGTH_SHORT).show();
            }else if(item.getItemId() == 2){
                Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
            }
           return super.onContextItemSelected(item);
    }

对话框

对话框是在当前界面弹出的一个小窗口,用于显示重要提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等。一般情况下,用户要与对话框进行交互,然后返回到被遮盖的界面以继续运行当前的应用程序。
AlertDialog常用方法
要创建一个AlertDialog就要用到AlertDialog.Builder中的create()方法
1. setTitle:为对话框设置标题
2. setIcon:为对话框设置图标
3. setMessage:为对话框设置内容
4. setView:为对话框设置自定义样式
5. setItems:设置对话框要显示的一个list
6. setMutiChoiceItems:设置对话框显示一系列的复选框
7. setSingleChoiceItems:设置单选按钮
8. setNeutralButton:普通按钮
9. setPositiveButton:确认按钮
10. setNegativeButton:取消按钮
ProgressDialog

MainActivity.java
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.button1){
            //默认对话框
            onCreateNormalDialog();
        }else if(id==R.id.button2){
            //单选按钮对话框
            onCreateSingleChoiceItems();
        }else if(id==R.id.button3){
            //多选按钮对话框
            onCreateMutiChoiceItems();
        }else if(id==R.id.button4){
            //列表按钮对话框
            onCreateItems();
        }else if(id==R.id.button5){
            //自定义对话框
            createDialog();
        }else if(id==R.id.button6){
            CreateProgressDialog();
        }
    }
    private void CreateProgressDialog() {
        final ProgressDialog pd=ProgressDialog.show(this, "搜索网络", "请耐心等待...");
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //结束ProgressDialog
                pd.dismiss();
            }
        }).start();
    }

    private void createDialog() {
        Builder builder=new Builder(this);
        builder.setTitle("注册");
        //自定义布局
        View view =LayoutInflater.from(this).inflate(R.layout.register, null);
        builder.setView(view);
        builder.setPositiveButton("确定", null);
        builder.setNegativeButton("取消", null);
        builder.create();
        builder.show();
    }
    private void onCreateItems() {
        Builder b=new Builder(this);

        b.setIcon(R.drawable.ic_launcher);
        b.setTitle("部门成员列表");
        String items[]={"项目经理","策划","测试","美工","程序员"};
        b.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
              Log.d("Tag", "which="+which);
            }
        });

        b.create();
        b.show();
    }

    private void onCreateMutiChoiceItems() {
        Builder b=new Builder(this);
        b.setTitle("爱好");
        b.setIcon(R.drawable.ic_launcher);

        String items[]={"篮球","足球","乒乓球","排球"};

        b.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Log.d("Tag", "which="+which+",isChecked="+isChecked);
            }
        });
        b.create();
        b.show();
    }
    private void onCreateSingleChoiceItems() {
        Builder builder =new Builder(this);

        builder.setTitle("请选择性别");
        builder.setIcon(R.drawable.ic_launcher);
        String items[]={"男","女","保密"};
        builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("Tag", "which="+which);
            }
        });

        builder.create();
        builder.show();
    }
    private void onCreateNormalDialog() {
        Builder builder=new Builder(this);

        builder.setTitle("退出");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMessage("确定要退出吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setNeutralButton("中间", null);
        builder.setNegativeButton("取消", null);
        builder.create();
        builder.show();
    }
}

Register.xml

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:hint="输入用户名">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword"
        android:hint="输入密码" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" 
        android:hint="输入密码"/>

</LinearLayout>

activity_main.xml

<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.android_829_alertdialog.MainActivity" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通对话框" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="单选按钮对话框" />
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="多选按钮对话框" />
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="列表对话框" />
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义对话框" />
        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="进度条对话框" />
    </LinearLayout>
</RelativeLayout>

PopupWindow
PopupWindow这个类用来实现一个弹出框,可以使用任意布局View作为其内容,这个弹出框悬浮在当前activity之上。
构造PopupWindow
注意:生成一个PopupWindow必须设置三个条件:View contentView,int Width,int height, 少任意一个就不能弹出PopupWindow。

作者:u013238646 发表于2016/9/4 16:02:18 原文链接
阅读:94 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>