Button是Textview 的子类
自己定义按钮形状
Drawable-new drawable resource file-root element(shape)-bg_btn2 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid//填充 android:color="#FF9900"/> <stroke//描边 android:width="1dp" Android:color="FFFFFF"/> <corners//圆角 android:radius="20dp"/> </shape> 然后在layout中 android:background="@drawable/bg_btn2"
按压效果
Drawable-new drawable resource file-root element(selector)-bg_btn3 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#03A9F4"/> <corners android:radius="10dp"/> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="#009688"/> <corners android:radius="10dp"/> </shape> </item> </selector>
点击事件
1. 方法1: 在activity中 public void showToast(View view){ Toast.makeText(this,"我被点击了",Toast.LENGTH_SHORT).show(); } 在layout中 android:onClick="showToast" 2. 方法2: mBtn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ButtonActivity.this,"我也被点击了",Toast.LENGTH_SHORT).show(); } });
更新不了的时候,把代理去掉。
一行显示不下,以……代替
Android:maxline="1"
Android:ellipsize=“end”
右侧添加图片(在layout中就可以了)
android:drawableRight="@drawable/a2"
在activity中给文本添加中划线/下划线
mTv2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线 mTv2.getPaint().setFlags(Paint.UNDERLINE_THRU_TEXT_FLAG);//下划线
去除锯齿
mTv2.getPaint().setAntiAlias(true);
界面跳转
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnTextView=findViewById(R.id.btn_textview);
mBtnTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转到TextView界面
Intent intent=new Intent(MainActivity.this,TextViewActivity.class);
startActivity(intent);
}
跑马灯效果
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mTv2.getPaint().setFlags(Paint.UNDERLINE_THRU_TEXT_FLAG);//下划线"
android:textSize="24sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
/>
EditText
<EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="50dp" android:textColor="#FFC107" android:layout_below="@id/et_1" android:layout_marginTop="10dp" android:textSize="20sp" android:drawableLeft="@drawable/passwor" android:drawablePadding="10dp" android:hint="密码" android:maxLength="1" android:inputType="textPassword"*显示 android:inputType="number" 呼出数字键 android:background="@drawable/bg_username" android:paddingLeft="10dp" /> 监控EditText输入(可以再LogCat中看到输入) mEtUserName=findViewById(R.id.et_1); mEtUserName.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("edittext",s.toString()); } @Override public void afterTextChanged(Editable s) { }
RadioButton
<RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/rg_1" android:padding="15dp"> <RadioButton android:id="@+id/rb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true"//默认选中要加ID android:textSize="12sp" android:textColor="#9C27B0" /> <RadioButton android:id="@+id/rb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textSize="12sp" android:textColor="#9C27B0" /> </RadioGroup>
页面跳转
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnTextView=findViewById(R.id.btn_textview); mBtnButton=findViewById(R.id.btn_button); mBtnEditText=findViewById(R.id.btn_edittext); mBtnRadioButton=findViewById(R.id.btn_radiobutton); setListeners(); } private void setListeners(){ OnClick onClick=new OnClick(); mBtnTextView.setOnClickListener(onClick); mBtnButton.setOnClickListener(onClick); mBtnEditText.setOnClickListener(onClick); mBtnRadioButton.setOnClickListener(onClick); } private class OnClick implements View.OnClickListener{ public void onClick(View v){ Intent intent=null; switch (v.getId()){ case R.id.btn_textview: intent=new Intent(MainActivity.this,TextViewActivity.class); //跳转TextView演示界面 break; case R.id.btn_button: intent=new Intent(MainActivity.this,ButtonActivity.class); //跳转TextView演示界面 break; case R.id.btn_edittext: intent=new Intent(MainActivity.this,EditTextActivity.class); //跳转TextView演示界面 break; case R.id.btn_radiobutton: intent=new Intent(MainActivity.this,RadioButtonActivity.class); //跳转TextView演示界面 break; } startActivity(intent); } }
在layout中添加listview.xml
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/list_item"//设置选中或未选中时的背景颜色
></ListView>
创建一个MyListAdapter
public class MyListAdapter extends BaseAdapter {
//适配器
private Context mContext;
private LayoutInflater mLayoutInflater;
public MyListAdapter(Context context){
this.mContext=context;
mLayoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return 10;//一般不是固定值
}
static class ViewHolder{
//提高复用?
public ImageView imageView;
public TextView tvTitle, tvTime, tvContent;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
convertView=mLayoutInflater.inflate(R.layout.layout_list_item,null);//单写一个layout_list_item设定每个view的格式
holder= new ViewHolder();
holder.imageView=convertView.findViewById(R.id.iv);
holder.tvContent=convertView.findViewById(R.id.tv_content);
holder.tvTime=convertView.findViewById(R.id.tv_time);
holder.tvTitle=convertView.findViewById(R.id.tv_title);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
//给控件赋值
holder.tvTitle.setText("这是标题");
holder.tvTime.setText("2019.12.28");
holder.tvContent.setText("这是内容啊,5555");
Glide.with(mContext).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577515195713&di=b9d28fc56629712f88c19c3b52a9a734&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201802%2F14%2F20180214135722_WJefw.jpeg").into(holder.imageView);
return convertView;
}
}
在LisviewActivity中用适配器
mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));
设置点击/长按事件
mLv1.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListViewActivity.this,"长按pos"+position,Toast.LENGTH_SHORT).show();
return true;//不会出现长按之后还有短按的效果
}
});