您的位置:首页 > 其它

AnalogClock、ToggleButton、Switch、RatingBar、SeekBar

2015-04-14 18:20 489 查看
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择开关:" />

<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:textOff="关闭"
android:textOn="开启" />

<Switch
android:id="@+id/sw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="关闭"
android:textOn="开启" />

<TextView
android:id="@+id/textts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_dark"
android:textSize="20sp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:text="打分" />

<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="6"
android:numStars="6"
android:progress="2"
android:stepSize="0.5" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />

<TextView
android:id="@+id/textrb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" />

<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>

</LinearLayout>


MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

private ImageView img;
private SeekBar seekbar;
private TextView text;
private RatingBar ratingbar;
private TextView textratingbar;
private ToggleButton togglebutton;
private Switch sw;
private TextView textts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.img);
seekbar=(SeekBar)findViewById(R.id.seekbar);
text=(TextView)findViewById(R.id.text);
//text可以滚动
text.setMovementMethod(ScrollingMovementMethod.getInstance());
seekbar.setOnSeekBarChangeListener(new MyOnSeekBarChange());
//SeekBar拖动条事件结束
//SeekBar评分开始
ratingbar=(RatingBar)findViewById(R.id.ratingbar);
textratingbar=(TextView)findViewById(R.id.textrb);
ratingbar.setOnRatingBarChangeListener(new MyOnRatingBarChang());
//状态开关
togglebutton=(ToggleButton)findViewById(R.id.togglebutton);
sw=(Switch)findViewById(R.id.sw);
textts=(TextView)findViewById(R.id.textts);
togglebutton.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
sw.setOnCheckedChangeListener(new MyOnCheckedChangeListener());

}
/**
* SeekBar拖动条事件
* @author Administrator
*
*/
private class MyOnSeekBarChange  implements OnSeekBarChangeListener{

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
text.append("显示中:"+seekBar.getProgress()+"\n");
//设置透明度
img.setImageAlpha(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
text.append("开始"+"\n");
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
text.append("结束"+"\n");
}

}
/**
* SeekBar评分
* @author Administrator
*
*/
private class MyOnRatingBarChang implements OnRatingBarChangeListener{

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
textratingbar.setText("用户评分:"+rating);
}

}
/**
* ToggleButton  Switch状态开关
* @author Administrator
*
*/
private class MyOnCheckedChangeListener implements OnCheckedChangeListener{

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
textts.setText("您的选择是:开");
}else {
textts.setText("您的选择是:关");
}
}

}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息