2015/9/2问题集
AlertDialog
AlertDialog是Dialog的一个直接子类
一个AlertDialog可以有两个Button或者3个Button
可以对一个AlertDialog设置title、message。
不能直接通过AlertDialog的构造函数来生成一个AlertDialog
一般生成的时候都是通过它的的一个内部静态类AlertDialog.Builder来构造的。
AlertDialog的使用 利用系统自带的进行设置
跟其他提示框一样要先创建成dialog,然后才可以show或者dismiss
package com.zsz.develop.alertdialogdemo;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send();
}
public void send()
{
// ....... 构造一个 AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 对话框标题
.setTitle("简单对话框")
// 对话框图标
.setIcon(R.mipmap.ic_launcher);
// 对话框内容
// .setMessage("对话框内容.");
// 添加一个按钮,并监听按钮事件 (积极的;确实的)
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
});
// 添加一个按钮,并监听按钮事件 (消极的;否认的)
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
});
// 添加一个按钮,并监听按钮事件 (中立的)
builder.setNeutralButton("帮助", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
});
// 列表
CharSequence chars[] = {"hello", "wrold"};
// 列表项目对话框
builder.setItems(chars, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击了第几列
Log.i("info", String.valueOf(which));
// ... 动作
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
});
// 单选按钮
builder.setSingleChoiceItems(chars, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击了第几列
Log.i("info", String.valueOf(which));
// ... 动作
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
});
boolean m_boolean[] = {false, false};
// 多选按钮
builder.setMultiChoiceItems(chars, m_boolean, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 获取点击事件,并进行记录
Log.i("info", String.valueOf(which) + " " + String.valueOf(isChecked));
}
});
// 获取一个 View
// View root = this.getLayoutInflater().inflate(R.layout.activity_control, null);
// 可以设置 View
// builder.setView(root);
// 创建及显示
builder.create().show();
// .......
}
}
CharSequence与String之间的区别
CharSequence与String都能用于定义字符串,但CharSequence的值是可读可写序列,而String的值是只读序列。
有message就没有item
还可以设置单选框多选框
单选框
.setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0,
多选框
.setMultiChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, null, null)
#
AlertDialog的使用 自定义设置
package com.zsz.develop.alertdialogdemo;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ControlActivity extends AppCompatActivity {
View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send();
}
});
}
public void send()
{
// 获取一个 View
root = this.getLayoutInflater().inflate(R.layout.activity_control, null);
// .......
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 对话框标题
.setTitle("简单对话框")
// 对话框图标
.setIcon(R.mipmap.ic_launcher);
// 对话框内容
// .setMessage("对话框内容.");
// 添加一个按钮,并监听按钮事件 (积极的;确实的)
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取结果
EditText edit = (EditText) root.findViewById(R.id.editText);
EditText edit2 = (EditText) root.findViewById(R.id.editText2);
Log.i("info", edit.getText().toString());
Log.i("info", edit2.getText().toString());
}
});
// 添加一个按钮,并监听按钮事件 (消极的;否认的)
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(ControlActivity.this, ControlActivity.class);
startActivity(intent);
}
});
// 可以设置 View
builder.setView(root);
// 创建及显示
builder.create().show();
// .......
}
}
布局文件
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.nljb.nljb.ControlActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn"
android:id="@+id/btn"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:id="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:ems="10"
android:id="@+id/editText2" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
drawerlayoutandroid 抽屉效果的导航菜单实现
Spinner的使用
package com.zsz.develop.spinnerdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String[] m={"A型","B型","O型","AB型","其他"};
private TextView view ;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (TextView) findViewById(R.id.spinnerText);
spinner = (Spinner) findViewById(R.id.Spinner01);
//将可选内容与ArrayAdapter连接起来
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter 添加到spinner中
spinner.setAdapter(adapter);
//添加事件Spinner事件监听
spinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) new SpinnerSelectedListener());
//设置默认值
spinner.setVisibility(View.VISIBLE);
}
//使用数组形式操作
class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
view.setText("你的血型是:"+m[arg2]);
//这是得到spinner里面的内容
// String selectedValue = (String)spType.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView android:id="@+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<Spinner android:id="@+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Spinner>
</LinearLayout>