TextView控件
第一:界面android 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=".MainActivity" >
<TextView
android:id="@+id/txtOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/txtTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world 直接显示" />
</RelativeLayout>
代码中的灰色代码就是赋值的方式,txtOne是利用资源文件夹中的res—values—strings.xml中来赋值
strings.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Demo1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
这样就可以现在手机屏幕显示出 hello world、hello world 直接显示 的字符串了。
第二:java代码后台实现
TextView txtOne,txtTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {//每个Activity创建的时候的入口函数
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOne=(TextView)this.findViewById(R.id.txtOne); //根据id获取到控件
txtTwo=(TextView)this.findViewById(R.id.txtTwo);
txtOne.setText("这个是给txtOne赋值");
txtOne.setText("这个是给txtTwo赋值");
}
这个是给txtOne赋值,这个是给txtTwo赋值 两个字符串
Button控件
一:监听事件
上面的TextView中介绍了对控件的显示值的控制,Button和TextView的显示值和它一样,下面我直接进行事件的监听演示
界面的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" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/btnDemo"
android:text="按钮一"
/>
</LinearLayout>
java端代码:
Button btnDemo;
@Override
protected void onCreate(Bundle savedInstanceState) {//每个Activity创建的时候的入口函数
super.onCreate(savedInstanceState);
btnDemo=(Button)this.findViewById(R.id.btnDemo);
btnDemo.setOnClickListener(new OnClickListener()); //添加按钮的监听事件,这里是实例化一个实现接口OnClickListener的类
}
private class OnClickListener implements android.view.View.OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==btnDemo){
Toast.makeText(MainActivity.this, "你点击了按钮", Toast.LENGTH_LONG).show(); //这个一个对话框,用的范围也比较多
//参数1:Context,参数2:弹出框的显示文本 参数3:显示的形式
}
}
}
ListView控件
首先建立一个xml文件 list_item.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="horizontal" >
<TextView
android:id="@+id/txtID"
android:layout_width="120px"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/txtName"
android:layout_width="120px"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnShow"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="显示姓名"
/>
</LinearLayout>
再建一个 listview.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" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
创建一个PersonBean的用来当做源数据
import java.util.ArrayList;
import java.util.List;
public class PersonBean {
private int ID;
private String Name;
public int getID() {
return ID;
}
public void setiD(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public static List<PersonBean> GetData() {
List<PersonBean> list=new ArrayList<PersonBean>();
for (int i = 0; i < 10; i++) {
PersonBean personBean=new PersonBean();
personBean.ID=i;
personBean.Name="ulimate"+i;
list.add(personBean);
}
return list;
}
}
自定义个适配器,用来当做绑定listview的桥梁
import java.util.List;
import java.util.Map;
import android.R.layout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater; //用来实例化布局
private List<Map<String, Object>> list; //数据源
Context context;
/**
* 构造函数
* @param context
* @param list
* @param LayoutID
* @param flag
* @param ItemIDs
*/
public MyAdapter(Context context,List<Map<String, Object>> list){
this.mInflater=LayoutInflater.from(context);
this.list=list;
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
/**
*
* @param postion 绘制的行数
* @param convertView listview每一行的布局
* @param parent view的集合,不设置就为null
*/
@Override
public View getView(int postion, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=mInflater.inflate(R.layout.list_item, null); //将布局的通过mInflater实例化为一个view
}
final TextView txtID=(TextView)convertView.findViewById(R.id.txtID); //获取list_item.xml中的txtid控件
txtID.setText(list.get(postion).get("ID").toString()); //赋值
final TextView txtName=(TextView)convertView.findViewById(R.id.txtName);//获取list_item.xml中的txtname控件
txtName.setText(list.get(postion).get("Name").toString());//赋值
final Button btnShow=(Button)convertView.findViewById(R.id.btnShow);//获取list_item.xml中的btnDemo控件
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==btnShow){
Toast.makeText(context, txtID.getText()+":"+txtName.getText(), Toast.LENGTH_LONG).show();
}
}
});
return convertView;
}
}
这样我们就做好了所有的准备,下面在Activity里面加入下面代码就可以实现listview的绑定,且每行按钮的点击事件
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {//每个Activity创建的时候的入口函数
super.onCreate(savedInstanceState);
MyAdapter myAdapter=new MyAdapter(this, getData());
setContentView(R.layout.listview);
lv=(ListView)this.findViewById(R.id.lv);
lv.setAdapter(myAdapter);
}
/**
* 获取数据
* @return
*/
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); //定义一个变量
List<PersonBean> listPerson=PersonBean.GetData(); //获取数据
for (PersonBean personBean : listPerson) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID", personBean.getID());
map.put("Name", personBean.getName());
list.add(map);
}
return list;
}
Dialog
对话框在各种应用或者游戏开发都会用到,当然也会以各种不同的方式出现。
第一种:Toast(他不会打断当前正在操作的应用,在屏幕上显示一段时间(2秒左右)后消失)。
代码如下:
Toast.makeText(this, "", Toast.LENGTH_LONG).show(); //参数1:Context对象 参数2:对话框提示的内同 参数3:对象框展示的方式
第二种:Builder
private Builder builder;
private TextView txtTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(new BitmapBodyView(this));
setContentView(R.layout.activity_main);
txtTextView=(TextView)this.findViewById(R.id.lab);
builder=new Builder(this);
builder.setIcon(R.drawable.ic_launcher); //设置图标
builder.setTitle("Dialog");//设置标题
builder.setMessage("I'm Dialog");//设置标题内容
builder.setPositiveButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
txtTextView.setText("点击了取消");
}
});
builder.setNegativeButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
txtTextView.setText("点击了确定");
}
});
builder.show();
}
Builder设置单选对话框:
private Builder builder;
private TextView txtTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(new BitmapBodyView(this));
setContentView(R.layout.activity_main);
txtTextView = (TextView) this.findViewById(R.id.lab);
builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher); // 设置图标
builder.setTitle("Dialog");// 设置标题
builder.setSingleChoiceItems(new String[] { "单选一", "单选二" }, 1, //为对话框设置单选框
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
txtTextView.setText("你选择了"+which);
}
});
builder.setPositiveButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
txtTextView.setText("点击了取消"+which);
}
});
builder.setNegativeButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
txtTextView.setText("点击了确定"+which);
}
});
builder.show();
}
Builder设置多选对话框:
private Builder builder;
private TextView txtTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(new BitmapBodyView(this));
setContentView(R.layout.activity_main);
txtTextView = (TextView) this.findViewById(R.id.lab);
builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher); // 设置图标
builder.setTitle("Dialog");// 设置标题
builder.setMultiChoiceItems(new String[] { "多选一", "多选二" },
new boolean[] { false, false },
new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
String msg=isChecked?"有":"没有";
txtTextView.setText("你"+msg+"选择了" + which);
}
});
builder.show();
}
Builder设置列表选择
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(new BitmapBodyView(this));
setContentView(R.layout.activity_main);
txtTextView = (TextView) this.findViewById(R.id.lab);
builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher); // 设置图标
builder.setTitle("Dialog");// 设置标题
builder.setItems(new String[]{"列表一","列表二"},new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.show();