Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android高級界面

android高級界面

編輯:關於Android編程

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, string="">() {
             //後台線程
             @Override
                protected String doInBackground(Integer... params) {
                     int num=params[0];
                     int i=0;
                     while(i<num){ catch="" count="values[0];" int="" integer...="" interruptedexception="" pre="" protected="" return="" string="" try="" void="">

Activity_main.xml


    
    
        
        
        
    

菜單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

   

activity_main.xml

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




    

        
    

    

    

activity_main.xml


    

PopupWindow
PopupWindow這個類用來實現一個彈出框,可以使用任意布局View作為其內容,這個彈出框懸浮在當前activity之上。
構造PopupWindow
注意:生成一個PopupWindow必須設置三個條件:View contentView,int Width,int height, 少任意一個就不能彈出PopupWindow。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved