以下是源代码,那个button按钮怎么美化呢?,请大家帮帮我吧。
比方说如何让button按钮有按下的效果,如何让button按钮实现颜色的渐变。
- Java code
package com.demo.android.bmi;import java.text.DecimalFormat;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class Bmi extends Activity { /** Called when the activity is first created. * @param <calcBMI>*/ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Listen for button clicks Button button = (Button)findViewById(R.id.submit); button.setOnClickListener(calcBMI); } private OnClickListener calcBMI = new OnClickListener(){ public void onClick(View v){ DecimalFormat nf =new DecimalFormat("0.00"); EditText fieldheight = (EditText)findViewById(R.id.height); EditText fieldweight = (EditText)findViewById(R.id.weight); double height = Double.parseDouble(fieldheight.getText().toString())/100; double weight = Double.parseDouble(fieldweight.getText().toString()); double BMI = weight / (height * height); TextView result = (TextView)findViewById(R.id.result); result.setText("Your BMI is "+nf.format(BMI)); //Give health advice TextView fieldsuggest = (TextView)findViewById(R.id.suggest); if(BMI>25){ fieldsuggest.setText(R.string.advice_heavy); } else if(BMI<20){ fieldsuggest.setText(R.string.advice_light); } else { fieldsuggest.setText(R.string.advice_average); } } }; }
大家一定要帮帮我呀,谢谢大家了。
------解决方案--------------------
设置按钮的background属性(引用XML文件) xml文件的 selector标签
可以百度下 android selector
------解决方案--------------------
在drawable文件夹下创建并保存。
btn_play.xml
<selectorxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:drawable="@drawable/img_btn_play_pressed" android:state_focused="true" android:state_pressed="true"/> <itemandroid:drawable="@drawable/img_btn_play_pressed" android:state_focused="false" android:state_pressed="true"/> <itemandroid:drawable="@drawable/img_btn_play_pressed" android:state_focused="true"/> <itemandroid:drawable="@drawable/img_btn_play" android:state_focused="false" android:state_pressed="false"/></selector>
在布局文件中引用btn_play.xml
<ImageButton
android:id="@+id/btnPlay" android:src="@drawable/btn_play" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@null"/>
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
学习了,边百度边学