国内好的设计网站推荐,甘肃省建设厅官方网站造价总站,长沙百度seo,wordpress配置七牛云cdn我将Android控件的Button和Checkbox的学习知识总结一下和大家共享 在Android开发中#xff0c;按钮和复选框是非常常用的控件 Button控件的基本使用方法很简单#xff0c;在布局文件中使用Button既可以了#xff0c;或者在java代码#xff1a;Button button … 我将Android控件的Button和Checkbox的学习知识总结一下和大家共享 在Android开发中按钮和复选框是非常常用的控件 Button控件的基本使用方法很简单在布局文件中使用Button既可以了或者在java代码Button button ButtonfindViewByIdR.id.button1; Checkbox也是一样的。 1、Button控件 按钮控件的最常见的是单击事件可以通过Button.setOnClickListener方法设置处理单机事件的监听器也可以在Button标签的android:onClick属性指定单击事件方法名。如果当前类实现了android.view.view.OnClickListener接口可以直接将this传入到setOnClickListener方法。 下面看例子 布局文件 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationvertical android:layout_widthfill_parentandroid:layout_heightfill_parent android:gravitycenter_horizontalButton android:idid/btnCommonButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:text我的按钮1 /Button android:idid/btnImageButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:text按钮android:gravitycenter android:backgrounddrawable/button1 /
/LinearLayout
java实现文件 public class Main extends Activity implements OnClickListener, OnTouchListener,OnFocusChangeListener, OnKeyListener
{private int value 1;private Button imageButton;private Button commonButton;private View lastFocusview;Overridepublic void onClick(View view){Button button (Button) view;if (value 1 button.getWidth() getWindowManager().getDefaultDisplay().getWidth())value -1;else if (value -1 button.getWidth() 100)value 1;button.setWidth(button.getWidth() (int) (button.getWidth() * 0.1)* value);button.setHeight(button.getHeight() (int) (button.getHeight() * 0.1)* value);}Overridepublic boolean onKey(View view, int keyCode, KeyEvent event){ if (KeyEvent.ACTION_DOWN event.getAction()){view.setBackgroundResource(R.drawable.button3);}else if (KeyEvent.ACTION_UP event.getAction()){view.setBackgroundResource(R.drawable.button2);}return false;}Overridepublic void onFocusChange(View view, boolean hasFocus){if (hasFocus){imageButton.setBackgroundResource(R.drawable.button2);}else{imageButton.setBackgroundResource(R.drawable.button1);}}Overridepublic boolean onTouch(View view, MotionEvent event){if (event.getAction() MotionEvent.ACTION_UP){view.setBackgroundResource(R.drawable.button1);}else if (event.getAction() MotionEvent.ACTION_DOWN){view.setBackgroundResource(R.drawable.button2);}return false;}Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);commonButton (Button) findViewById(R.id.btnCommonButton);imageButton (Button) findViewById(R.id.btnImageButton);commonButton.setOnClickListener(this);imageButton.setOnClickListener(this);imageButton.setOnTouchListener(this);imageButton.setOnFocusChangeListener(this);imageButton.setOnKeyListener(this);}
}也可以试试图文混排的按钮 布局文件
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationvertical android:layout_widthfill_parentandroid:layout_heightfill_parentLinearLayout android:orientationhorizontalandroid:layout_widthfill_parent android:layout_height120dpButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:drawableTopdrawable/star android:text按钮1 /Button android:layout_widthwrap_contentandroid:layout_heightwrap_content android:drawableBottomdrawable/starandroid:text按钮2 android:drawablePadding30dp /Button android:layout_widthwrap_contentandroid:layout_heightwrap_content android:drawableLeftdrawable/starandroid:text按钮3 /Button android:layout_widthwrap_contentandroid:layout_heightwrap_content android:drawableRightdrawable/starandroid:text按钮4 android:drawablePadding20dp //LinearLayoutButton android:idid/button android:layout_width200dpandroid:layout_height200dp android:layout_marginTop10dp /
/LinearLayout
java实现文件 public class Main extends Activity
{Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Button button (Button) findViewById(R.id.button);SpannableString spannableStringLeft new SpannableString(left);Bitmap bitmapLeft BitmapFactory.decodeResource(getResources(),R.drawable.image_left);ImageSpan imageSpanLeft new ImageSpan(bitmapLeft, DynamicDrawableSpan.ALIGN_BOTTOM);spannableStringLeft.setSpan(imageSpanLeft, 0, 4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);SpannableString spannableStringRight new SpannableString(right);Bitmap bitmapRight BitmapFactory.decodeResource(getResources(),R.drawable.image_right);ImageSpan imageSpanRight new ImageSpan(bitmapRight);spannableStringRight.setSpan(imageSpanRight, 0, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);button.append(spannableStringLeft);button.append(我的按钮);button.append(spannableStringRight);}
}接下来介绍一下ImageButton图片按钮其实和普通按钮一样差别在与不用文本显示而是图片 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationhorizontal android:layout_widthfill_parentandroid:layout_heightfill_parentImageButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:srcdrawable/button1_1 /ImageButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:srcdrawable/button2_1 /
/LinearLayout
这边补充介绍RadioButton选项按钮控件和ToggleButton开关状态按钮控件
pre namecode classhtml?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationvertical android:layout_widthfill_parentandroid:layout_heightfill_parentRadioGroup android:layout_widthwrap_contentandroid:layout_heightwrap_contentRadioButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:text选项1 /RadioButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:text选项2 /RadioButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:text选项3android:drawableLeftdrawable/star android:drawableTopdrawable/circleandroid:drawableRightdrawable/star android:drawableBottomdrawable/circle android:drawablePadding20dp //RadioGroup
/LinearLayout
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationhorizontal android:layout_widthfill_parentandroid:layout_heightfill_parentToggleButton android:layout_widthwrap_contentandroid:layout_heightwrap_content/ToggleButton android:idid/toggleButton android:layout_widthwrap_contentandroid:layout_heightwrap_content android:layout_marginLeft30dpandroid:textOff打开电灯 android:textOn关闭电灯 /
/LinearLayout 2、Checkbox控件 Checkbox复选框控件在这里采用动态加载的方式在xml布局文件的根节点不仅可以像LinearLayoutRelativeLayout一样的ViewGroup也可以直接使用View下面采用Checkbox标签。 Checkbox.xml; ?xml version1.0 encodingutf-8?
CheckBox xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:idid/checkbox android:layout_widthfill_parentandroid:layout_heightwrap_content /main.xml ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationvertical android:layout_widthfill_parentandroid:layout_heightfill_parentButton android:idid/button android:layout_widthwrap_contentandroid:layout_heightwrap_content android:text确定 /
/LinearLayoutjava文件 public class Main extends Activity implements OnClickListener
{private ListCheckBox checkboxs new ArrayListCheckBox();Overridepublic void onClick(View view){String s ;for (CheckBox checkbox : checkboxs){if (checkbox.isChecked())s checkbox.getText() \n;}if (.equals(s))s 您还没选呢;new AlertDialog.Builder(this).setMessage(s).setPositiveButton(关闭, null).show();}Overridepublic void onCreate(Bundle savedInstanceState){String[] checkboxText new String[]{ 是学生吗, 是否喜欢Android, 买车了吗, 打算出国吗 };super.onCreate(savedInstanceState);LinearLayout linearLayout (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);for (int i 0; i checkboxText.length; i){CheckBox checkbox (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);checkboxs.add(checkbox);checkboxs.get(i).setText(checkboxText[i]);linearLayout.addView(checkbox, i);}setContentView(linearLayout);Button button (Button) findViewById(R.id.button);button.setOnClickListener(this);}
}