一个自定义控件的Demo

时间:2024-05-18 23:36:14

里面包括Button、Checkbock、listview、popupwindow的自定义

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button; public class AndroidWidgetActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
public void tolistmain(View view){
Intent intent = new Intent(this, ListMainActivity.class);
startActivity(intent);
}
public void topopwindowmain(View view){
Intent intent = new Intent(this, PopwindowMainActivity.class);
startActivity(intent);
}
}
import java.util.List;
import cn.ittest.domain.item;
import cn.ittest.widget.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class ListAdapter extends BaseAdapter { List<item> items;
private int resource;
private LayoutInflater inflater;
public ListAdapter(Context context,List<item> items,int resource) {
this.resource = resource;
this.items = items;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
return items.size();
} @Override
public Object getItem(int position) {
return items.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = null;
TextView titleView = null;
TextView expView = null;
if(convertView==null){
convertView = inflater.inflate(resource, null);//生成条目界面对象
imgView = (ImageView) convertView.findViewById(R.id.img);
titleView = (TextView) convertView.findViewById(R.id.tvtitle);
expView = (TextView) convertView.findViewById(R.id.tvexp); ViewCache cache = new ViewCache();
cache.imgView = imgView;
cache.titleView = titleView;
cache.expView = expView;
convertView.setTag(cache);
}else{
ViewCache cache = (ViewCache) convertView.getTag();
imgView = cache.imgView;
titleView = cache.titleView;
expView = cache.expView;
}
item listItem = items.get(position);
imgView.setImageResource(R.drawable.listtitle);
titleView.setText(listItem.getTitle());
expView.setText(listItem.getExp()); return convertView;
} private final class ViewCache{
public ImageView imgView;
public TextView titleView;
public TextView expView;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast; public class PopwindowMainActivity extends Activity{ private int screenx = 0;
private int screeny = 0;
private int x=0,y=0;
private EditText edtx;
private EditText edty;
private TextView showxy;
private Button sure;
private Button cancle;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop);
edtx = (EditText)this.findViewById(R.id.edtx);
edty = (EditText)this.findViewById(R.id.edty);
showxy = (TextView)this.findViewById(R.id.showxy);
sure = (Button)this.findViewById(R.id.sure);
cancle = (Button)this.findViewById(R.id.cancle);
showscreen();
innit();
}
public void showscreen(){
screenx = getWindowManager().getDefaultDisplay().getWidth();//获得屏幕宽度
screeny = getWindowManager().getDefaultDisplay().getHeight();//获得屏幕高度
showxy.setText("X:"+screenx+" "+"Y:"+screeny);
}
public void innit(){
View popitem = getLayoutInflater().inflate(R.layout.popwindowitem,null);
popupWindow = new PopupWindow(popitem,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);//初始化窗口
popupWindow.setFocusable(true);//
sure.setOnClickListener(new onpopsureclicklistener());
cancle.setOnClickListener(new onpopcancleclicklistener());
final EditText edtshow = (EditText)popitem.findViewById(R.id.edtshow);
Button itemsure = (Button)popitem.findViewById(R.id.itemsure);
Button itemcancle = (Button)popitem.findViewById(R.id.itemcancle);
itemsure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String showString = edtshow.getText().toString();
if (showString != null) {
Toast.makeText(getApplicationContext(), showString, Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}else {
Toast.makeText(getApplicationContext(), "输入为空,请重新输入!", Toast.LENGTH_SHORT).show();
}
}
});
itemcancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow != null) {
popupWindow.dismiss();
}
}
});
}
public final class onpopsureclicklistener implements View.OnClickListener{ @Override
public void onClick(View v) {
try {
x = Integer.parseInt(edtx.getText().toString());
y = Integer.parseInt(edty.getText().toString());
popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.LEFT|Gravity.TOP, x, y);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "输入的数据无效,请重新输入!", 1).show();
}
}
}
public final class onpopcancleclicklistener implements View.OnClickListener{ @Override
public void onClick(View v) {
if (popupWindow != null) {
popupWindow.dismiss();
}
}
}
}
public class item {
private String title;
private String exp;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getExp() {
return exp;
}
public void setExp(String exp) {
this.exp = exp;
}
public item(String title, String exp) {
super();
this.title = title;
this.exp = exp;
}
public item() {
}
@Override
public String toString() {
return "item [title=" + title + ", exp=" + exp + "]";
}
}
import java.util.ArrayList;
import java.util.List;
import cn.ittest.adapter.ListAdapter;
import cn.ittest.domain.item;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast; public class ListMainActivity extends Activity{ private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
list = (ListView)this.findViewById(R.id.list);
list.setOnItemClickListener(new onItemclicklistener());
show();
}
public final class onItemclicklistener implements OnItemClickListener{ @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listView = (ListView)parent;
item liItem = (item) listView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), liItem.toString(), 1).show();
}
}
public void show(){
List<item> items = new ArrayList<item>();
for (int i = 1; i <= 15; i++) {
item lItem = new item("第"+i+"个标题", "详细介绍神马!!!!!!!!!!!!!!!");
items.add(lItem);
}
ListAdapter adapter = new ListAdapter(this, items, R.layout.listitem);
list.setAdapter(adapter);
}
}

一个自定义控件的Demo一个自定义控件的Demo一个自定义控件的Demo一个自定义控件的Demo一个自定义控件的Demo一个自定义控件的Demo



地址:http://download.****.net/detail/u011566000/5865305