<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingLeft="10.0dip" android:paddingTop="10.0dip" android:paddingBottom="10.0dip" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginLeft="8dip" android:layout_gravity="center_vertical" android:paddingLeft="5.0dip" android:paddingTop="10.0dip" android:paddingBottom="10.0dip" android:paddingRight="10.0dip" android:textSize="18.0sp" /> </LinearLayout>
package com.szy.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CustomButton extends LinearLayout
{
private ImageView iv;
private TextView tv;
public CustomButton(Context context)
{
this(context, null);
}
public CustomButton(Context context, AttributeSet attrs)
{
super(context, attrs);
// 导入布局
LayoutInflater.from(context).inflate(R.layout.custom_button, this, true);
iv = (ImageView) findViewById(R.id.iv);
tv = (TextView) findViewById(R.id.tv);
}
/**
* 设置图片资源
*/
public void setImageResource(int resId)
{
iv.setImageResource(resId);
}
/**
* 设置显示的文字
*/
public void setTextViewText(String text)
{
tv.setText(text);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.szy.customview.CustomButton android:id="@+id/bt_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" /> <com.szy.customview.CustomButton android:id="@+id/bt_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" /> </LinearLayout>
package com.szy.customview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
private CustomButton btnConfirm;
private CustomButton btnCancel;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnConfirm = (CustomButton) findViewById(R.id.bt_confirm);
btnCancel = (CustomButton) findViewById(R.id.bt_cancel);
btnConfirm.setTextViewText("确定");
btnConfirm.setImageResource(R.drawable.confirm);
btnCancel.setTextViewText("取消");
btnCancel.setImageResource(R.drawable.cancel);
btnConfirm.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 在这里可以实现点击事件
}
});
}
}
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" /> <ImageButton android:id="@+id/ib" android:visibility="gone" android:src="@drawable/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:layout_alignRight="@+id/et" /> </RelativeLayout>
package com.szy.customview;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class CustomEditView extends LinearLayout implements EdtInterface
{
ImageButton ib;
EditText et;
public CustomEditView(Context context)
{
super(context);
}
public CustomEditView(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true);
init();
}
private void init()
{
ib = (ImageButton) findViewById(R.id.ib);
et = (EditText) findViewById(R.id.et);
et.addTextChangedListener(tw);// 为输入框绑定一个监听文字变化的监听器
// 添加按钮点击事件
ib.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
hideBtn();// 隐藏按钮
et.setText("");// 设置输入框内容为空
}
});
}
// 当输入框状态改变时,会调用相应的方法
TextWatcher tw = new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub
}
// 在文字改变后调用
@Override
public void afterTextChanged(Editable s)
{
if (s.length() == 0)
{
hideBtn();// 隐藏按钮
} else
{
showBtn();// 显示按钮
}
}
};
@Override
public void hideBtn()
{
// 设置按钮不可见
if (ib.isShown())
ib.setVisibility(View.GONE);
}
@Override
public void showBtn()
{
// 设置按钮可见
if (!ib.isShown())
{
ib.setVisibility(View.VISIBLE);
}
}
}
interface EdtInterface
{
public void hideBtn();
public void showBtn();
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.szy.customview.CustomEditView android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有