功能很好实现,但是用到的设计模式没有搞清楚,有一句话没太懂:每个调节项目调节时触发的动作会修改,不知道是什么含义,有明白的童鞋指导一下,选用何种设计模式。
下面看一下功能代码:
1.MainActivity.java:
package com.yayun.zhangyingtest;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class CopyOfMainActivity extends Activity implements OnClickListener,SetValue {
private LinearLayout mLinearLayoutTVs;
private Button btn_color, btn_light, btn_saturation;
private TextView tv_color_value, tv_light_value, tv_saturation_value;
private SeekBar seekBar;
private int i=0;
private final int TVLightId=0x001;
private final int TVColorId=0x002;
private final int TVSaturationId=0x003;
private final int BtnLightId=0x100;
private final int BtnColorId=0x200;
private final int BtnSaturationId=0x300;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
seekBar = (SeekBar) findViewById(R.id.seekbar);
mLinearLayoutTVs = (LinearLayout) findViewById(R.id.ll_tvs);
AddWidget("亮度", "80", tv_light_value, btn_light,BtnLightId,TVLightId);
AddWidget("色彩", "100", tv_color_value, btn_color,BtnColorId,TVColorId);
AddWidget("饱和度", "90", tv_saturation_value, btn_saturation,BtnSaturationId,TVSaturationId);
btn_color=(Button)findViewById(BtnColorId);
btn_light=(Button)findViewById(BtnLightId);
btn_saturation=(Button)findViewById(BtnSaturationId);
tv_color_value=(TextView)findViewById(TVColorId);
tv_light_value=(TextView)findViewById(TVLightId);
tv_saturation_value=(TextView)findViewById(TVSaturationId);
seekBar.setProgress(getProgress(tv_light_value));
btn_color.setOnClickListener(this);
btn_light.setOnClickListener(this);
btn_saturation.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(i==0){
tv_light_value.setText(String.valueOf(progress));
}else if(i==1){
tv_color_value.setText(String.valueOf(progress));
}else{
tv_saturation_value.setText(String.valueOf(progress));
}
}
});
}
private void AddWidget(String textViewName, String textViewValue,
TextView tv_value, Button btnId,int btnIntID,int tvIntID) {
LinearLayout.LayoutParams lp3=new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
LinearLayout ll_tvsLayout = (LinearLayout) findViewById(R.id.ll_tvs);
LinearLayout ll_btnsLayout = (LinearLayout) findViewById(R.id.ll_btns);
final LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
TextView tv_name = new TextView(this);
tv_name.setText(textViewName + ":");
tv_value = new TextView(this);
tv_value.setId(tvIntID);
tv_value.setText(textViewValue);
btnId = new Button(this);
btnId.setText(textViewName);
btnId.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
btnId.setId(btnIntID);
layout2.addView(tv_name);
layout2.addView(tv_value);
ll_tvsLayout.addView(layout2);
ll_btnsLayout.addView(btnId);
}
private int getProgress(TextView tv) {
return Integer.parseInt(tv.getText().toString());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case BtnLightId:
i=0;
seekBar.setProgress(getProgress(tv_light_value));
break;
case BtnColorId:
i=1;
seekBar.setProgress(getProgress(tv_color_value));
break;
case BtnSaturationId:
i=2;
seekBar.setProgress(getProgress(tv_saturation_value));
break;
default:
break;
}
}
@Override
public void SetLight(int value) {//实设预留方法
// TODO Auto-generated method stub
}
@Override
public void SetColor(int value) {
// TODO Auto-generated method stub
}
@Override
public void SetSaturature(int value) {
// TODO Auto-generated method stub
}
}
2.布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll_tvs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:orientation="vertical" >
</LinearLayout>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_btns"
android:max="100" />
<LinearLayout
android:id="@+id/ll_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
运行如下:
但是没有达到题目要求的,用设计模式实现,求大神指导!!多谢
标签:试题,tv,value,一道,int,private,zhangying,android,LinearLayout From: https://blog.51cto.com/u_15866446/5844742