public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); findViewById(R.id.button5).setOnClickListener(this); findViewById(R.id.button6).setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: btn1Click(); break; case R.id.button2: btn2Click(); break; case R.id.button3: btn3Click(); break; case R.id.button4: btn4Click(); break; case R.id.button5: btn5Click(); break; case R.id.button6: btn6Click(); btn7Click(); break; default: break; } } private void btn7Click()//评分条 { RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar1); ratingBar.setNumStars(5); ratingBar.setRating((float) 0.5);//默认显示的星星数 ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) { Log.e("RatingBar", "onRatingChanged:" + rating); } }); } private void btn6Click()//可操作进度条 { SeekBar seekBar= (SeekBar) findViewById(R.id.seekBar1); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar)//停止拖动 { Log.e("SeekBar", "onStopTrackingTouch"); } @Override public void onStartTrackingTouch(SeekBar seekBar)//开始拖动 { Log.e("SeekBar", "onStartTrackingTouch"); } @Override//进度改变 public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { Log.e("SeekBar", "onProgressChanged"); } }); } private int progress = 0; private void btn5Click()//进度条 { ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2); progressBar.setProgress(progress++); progressBar.setMax(100); } private void btn4Click()// 单选按钮 { RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup1); int id = radioGroup.getCheckedRadioButtonId(); RadioButton radioButton = (RadioButton) findViewById(id); String str = radioButton.getText().toString(); TextView textView = (TextView) findViewById(R.id.textView1); switch (id) { case R.id.radio0: textView.setText(str); break; case R.id.radio1: textView.setText(str); break; case R.id.radio2: textView.setText(str); break; default: break; } } private void btn3Click()//设置圆形进度条消失(不占位置) { findViewById(R.id.progressBar1).setVisibility(View.GONE); } private void btn2Click()//设置圆形进度条为隐形(原位置空白) { findViewById(R.id.progressBar1).setVisibility(View.INVISIBLE); } private void btn1Click()//复选框 { CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1); CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2); CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3); TextView textView = (TextView) findViewById(R.id.textView1); StringBuffer str = new StringBuffer(); if(checkBox1.isChecked()) { str.append(checkBox1.getText()); } if(checkBox2.isChecked()) { str.append(checkBox2.getText()); } if(checkBox3.isChecked()) { str.append(checkBox3.getText()); } textView.setText(str); } }
1.代码实现按钮点击方法
button.PerformClick()
注:只有当button.Enabled为true
==============================UI控件属性相关==============================
控件自定义:
1.圆形progressbar
系统styles里找到progressbar的style属性:
其中,下面这句决定背景图案的设置,这个属性添加到控件的属性里:
- @android:drawable/progress_medium_white
将系统的drawable文件夹找到progress_medium_white.xml复制到自己的项目里,并进行修改:
2.进度progressbar
系统style文件内容:
关联的属性为:
name="android:progressDrawable"
修改progress_horizontal.xml:
控件基本属性:
(1)修改属性style
style="?android:attr/progressBarStyleHorizontal"
(2)最大进度值为100
android:max="100"
(3)初始化的进度值
android:secondaryProgress="70"
(4)设置为无限进度
android:indeterminate="true"
(5)代码设置样式
ProgressBar progressBar = new ProgressBar(this); progressBar.setIndeterminate(false); progressBar.setProgressDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal)); progressBar.setIndeterminateDrawable(getResources().getDrawable(android.R.drawable.progress_indeterminate_horizontal)); progressBar.setMinimumHeight(20);
3.seekbar:
类似于progressbar,只是多了个拖动按钮。
添加一个thumb属性:实际上是个selector的按钮。
4.ratingbar:
搜索ratingbar的xml文件进行修改,关联属性:progressDrawable。
以4.2版本里ratingbar_full_holo_dark的风格为例:
ratingbar_full_empty_holo_dark.xml代码:
ratingbar_full_filled_holo_dark.xml代码:
rating_style.xml代码:
XML文件代码:
5.checkbox和radiobutton:
checkbox可以直接添加一个属性修改为star风格:
android:style="?android:attr/starStyle"
关联属性:
android:button="@drawable/checkbox_selector"
XML代码:
selector代码:
监听事件:
mIv_CheckXieyi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { Log.e("", "updateCheckBox===false"); } else { Log.e("", "updateCheckBox===true"); } } });
注:
左侧的图案直接使用button无法出现时,可使用drawableLeft设置,如下:
其中tab_title_maproute.xml代码如下:
其中selector_route_walk.xml代码如下:
6.TextView相关:
(1)DrawableTop在代码中的实现方法:
public View getView(int position, View convertView, ViewGroup parent){ LayoutInflater inflater = getLayoutInflater(); TextView textView = null; if (position == 0 || position == 2 || position == 8) { textView = (TextView) inflater.inflate( R.layout.navi_menu_item_separator, null); } else { textView = (TextView) inflater.inflate( R.layout.navi_menu_item, null); Drawable drawable = getResources().getDrawable(ICONS[position]); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textView.setCompoundDrawables(drawable, null, null, null);//四个参数分别对应为上下左右,相当于xml里对textview设置drawabletop } textView.setText(TITLES[position]); return textView;}
(2)文本添加链接功能的属性autolink:
@Overrideprotected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.link); /* * APIdemo里:com.example.android.apis.text; */ SpannableString ss = new SpannableString("text4: Manually created spans. Click here to dial the phone."); ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // setSpan方法可以用来根据判断文本位置设置文本特定类型 ss.setSpan(new URLSpan("tel:4155551212"), 31 + 6, 31 + 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); TextView t4 = (TextView) findViewById(R.id.text4); t4.setText(ss); t4.setMovementMethod(LinkMovementMethod.getInstance());}
(3)为文字加阴影
(4)添加下划线
如果是在资源文件里,可以这样写:
phone: 1390123456 MyLink
如果是代码这样写.
TextView textView = (TextView)findViewById(R.id.testView); textView.setText(Html.fromHtml(""+"hahaha"+""));
或者也可以这样写:
textview.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
(5)通过字符串格式拼凑文本
String content = TextUtil.preventEmpty(comment.content); String replyToUserName = comment.replyToUser.username; String ContentBody = APP.getInstance().getString(R.string.discuss_content,replyToUserName,content); tvDiscussContent.setText(ContentBody);
xml资源内写法:
回复 %1$s : %2$s
(6)设置部分字体颜色
textView = (TextView) findViewById(R.id.textview); SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString()); //ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色 ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED); ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE); ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE); ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN); ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW); builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE); builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(builder);
(7)设置style:
textView.setTextAppearance(mContext, R.style.labels);
(8)长按复制,api>11
android:textIsSelectable="true"
6.EditText相关:
1.设置默认提示:
android:hint="请输入姓名"android:textColorHint="#ff00ff00"
android:background="@null"去掉输入框
2.取消焦点和请求焦点方法
//取消焦点 mEt_login_name.setFocusable(false);
//请求焦点 mEt_login_name.setFocusableInTouchMode(true); mEt_login_name.setFocusable(true); mEt_login_name.requestFocus();
3.监听编辑框字数
// 字数变化 mEt_content.addTextChangedListener(this); onTextChanged(mEt_content.getText(), 0, mEt_content.length(), 0);
/** * ******************监听编辑框输入字数********************************** */ @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { int remain = MAX_TEXT_COUNT - mEt_content.length(); mTv_counter.setText(String.valueOf(remain)); mTv_counter.setTextColor(remain > 0 ? 0xffcfcfcf : 0xffff0000); }
4.监听编辑框输入回车键
mEt_jianhuo.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (KeyEvent.KEYCODE_ENTER == keyCode && event.getAction() == KeyEvent.ACTION_DOWN) { Log.e("mEt_jianhuo", "enter"); saoMaCheckedToServer(mEt_jianhuo.getText().toString() + "", mData.getId() + "", isUseAvg()); return true; } return false; } });
5.设置软键盘回车键显示为"下一条"或者"完成"等
主要属性:
imeActionLabel
imeOptions
singleLine