0

我在我的应用程序中自定义了搜索栏。我也可以拖动搜索栏并访问未实现的方法。我的代码是

public class Income extends Activity implements OnSeekBarChangeListener, OnItemSelectedListener 
{ 
   SeekBar net_sales1,cogs,operating,other_revenue;
   public void onCreate(Bundle bundle)
   {
       super.onCreate(bundle);
       setContentView(R.layout.income);
       seek_txt1 = (TextView)findViewById(R.id.net_sales);
       seek_txt2 = (TextView)findViewById(R.id.cogs_te);
       seek_txt3 = (TextView)findViewById(R.id.operate_exp);
       seek_txt4 = (TextView)findViewById(R.id.other_rev);
       net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
       cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
       operating = (SeekBar)findViewById(R.id.mySeekingBar3);
       other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
       net_sales1.setOnSeekBarChangeListener(this);
       cogs.setOnSeekBarChangeListener(this);
       operating.setOnSeekBarChangeListener(this);
       other_revenue.setOnSeekBarChangeListener(this);
   }

   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
   {
       seek_count=0;
       progress1=progress-150;

       verify = fromUser;

       if (fromUser) 
       {
           if (progress >= 0 && progress <= seekBar.getMax())
           {                        

                progressString  = String.valueOf(10*progress-150);

           }
       }
       seek_id=seekBar.getId();
       switch(current_spinner)

       {
            case R.id.mySeekingBar1 :   
                //tablelay.removeView(tableLayout);


                seek_txt1.setText(progressString);
                int seekbar1_val = Integer.parseInt(seek_txt1.getText().toString());
                progress_of_spinner1 = progress;

                break;
            case R.id.mySeekingBar2 :   
                 seek_txt2.setText(progressString);
                 progress_of_spinner2 = progress;
                 break;
           case R.id.mySeekingBar3 :   
                 seek_txt3.setText(progressString);
                 progress_of_spinner3 = progress;
                 break;                                  
           case R.id.mySeekingBar4 :   
                 seek_txt4.setText(progressString);
                 progress_of_spinner4 = progress;
                 break;
         }


         net_sales1.setProgress(progress_of_spinner1);
         cogs.setProgress(progress_of_spinner2);
         operating.setProgress(progress_of_spinner3);
         other_revenue.setProgress(progress_of_spinner4);

    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) 
    {
        int progress2 = progress1;

        originalProgress=seekBar.getProgress();

    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) 
    {
        progress2=progress1;


    }

现在我的问题是,是否可以在活动的 onCreate() 方法中访问所有未实现的搜索栏方法?有没有机构可以告诉我?提前致谢。

4

2 回答 2

1

你想用在

创建

比你需要删除

实现 OnSeekBarChangeListener

和下线

net_sales1.setOnSeekBarChangeListener(this);
   cogs.setOnSeekBarChangeListener(this);
   operating.setOnSeekBarChangeListener(this);
   other_revenue.setOnSeekBarChangeListener(this);

并给出单独的

OnSeekBarChangeListener

比你在 onCreate 中使用的所有搜索栏

但是在这对于所有搜索栏,所有方法都单独覆盖。

请参阅下面的代码

 net_sales1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
   {
       @Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub

}
 });
     cogs.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
   {
       @Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub

}
});
   operating.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
   {
       @Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub

}
});
   other_revenue.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
   {
      @Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub

}
});
于 2012-11-03T05:35:49.087 回答
1

是的,它可能,如下使用:

将 onSeekBarChangeListener 设置为所有搜索栏,并提供未实现内联的定义。如下:

public class Income extends Activity implements OnItemSelectedListener 
{ 
   SeekBar net_sales1,cogs,operating,other_revenue;
   public void onCreate(Bundle bundle)
   {
       super.onCreate(bundle);
       setContentView(R.layout.income);
       seek_txt1 = (TextView)findViewById(R.id.net_sales);
       seek_txt2 = (TextView)findViewById(R.id.cogs_te);
       seek_txt3 = (TextView)findViewById(R.id.operate_exp);
       seek_txt4 = (TextView)findViewById(R.id.other_rev);
       net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
       cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
       operating = (SeekBar)findViewById(R.id.mySeekingBar3);
       other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
       net_sales1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
       {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
           {
                 //Do some operation in this block
           }
       });
       cogs.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
       {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
           {
                 //Do some operation in this block
           }
       });
       operating.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
       {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
           {
                 //Do some operation in this block
           }
       });// Do same as cogs
       other_revenue.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
       {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
           {
                 //Do some operation in this block
           }
       });//Do Same as cogs
   }
}

在 onCreate 方法中创建侦听器对象的另一个选项,并将此侦听器设置为所有 seekbar,如下所示:

public class Income extends Activity implements OnItemSelectedListener 
{ 
   SeekBar net_sales1,cogs,operating,other_revenue;
   public void onCreate(Bundle bundle)
   {
       super.onCreate(bundle);
       setContentView(R.layout.income);
       seek_txt1 = (TextView)findViewById(R.id.net_sales);
       seek_txt2 = (TextView)findViewById(R.id.cogs_te);
       seek_txt3 = (TextView)findViewById(R.id.operate_exp);
       seek_txt4 = (TextView)findViewById(R.id.other_rev);
       net_sales1 = (SeekBar)findViewById(R.id.mySeekingBar1);
       cogs = (SeekBar)findViewById(R.id.mySeekingBar2);
       operating = (SeekBar)findViewById(R.id.mySeekingBar3);
       other_revenue = (SeekBar)findViewById(R.id.mySeekingBar4);
       OnSeekBarChangeListener listener= new OnSeekBarChangeListener()
       {
           @Override
           public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
           {
                 //Do some operation in this block
           }
       });
       net_sales1.setOnSeekBarChangeListener(listener);
       cogs.setOnSeekBarChangeListener(listener);
       operating.setOnSeekBarChangeListener(listener);// Do same as cogs
       other_revenue.setOnSeekBarChangeListener(listener);//Do Same as cogs
   }
}
于 2012-11-03T05:25:33.903 回答