2017-02-23 166 views
0

我做了一个秒表,使用四个按钮的天文钟,但是当我使用可见性模式,使停止和暂停按钮出现它们重叠....请说明原因...下面是代码.....按钮重叠

假设在相对布局按钮布局文件...

public class StopWatchFragment extends Fragment { 

Chronometer chronometer; 
Button startStopWatch; 
Button stopStopWatch; 
Button resetStopWatch; 
Button pauseStopWatch; 
Button resumeStopWatch; 
private long lastPause; 
//RelativeLayout relativeLayout; 
private int check = 0; 

public StopWatchFragment() { 
    // Required empty public constructor 
} 



@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false); 
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch); 
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch); 
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch); 
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch); 
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch); 
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch); 
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout); 
pauseStopWatch.setVisibility(View.GONE); 

//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams()); 

startStopWatch.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     chronometer.setBase(SystemClock.elapsedRealtime()); 
     chronometer.start(); 
     startStopWatch.setVisibility(View.GONE); 
     pauseStopWatch.setVisibility(View.VISIBLE); 
     if(check == 1){ 
      resumeStopWatch.setClickable(true); 
     } 
     else{ 
      resumeStopWatch.setClickable(false); 
     } 
     //params.setMargins(16,16,16,16); 
     //pauseStopWatch.setLayoutParams(params); 
    } 
}); 

pauseStopWatch.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     check = 1; 
     lastPause = SystemClock.elapsedRealtime(); 
     chronometer.stop(); 
    } 
}); 

resumeStopWatch.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause); 
     chronometer.start(); 
     resumeStopWatch.setClickable(false); 
    } 
}); 

stopStopWatch.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     chronometer.stop(); 
     startStopWatch.setVisibility(View.VISIBLE); 
     pauseStopWatch.setVisibility(View.GONE); 
     resumeStopWatch.setClickable(false); 
    } 
}); 

resetStopWatch.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     chronometer.setBase(SystemClock.elapsedRealtime()); 
     chronometer.stop(); 
     resumeStopWatch.setClickable(false); 
     startStopWatch.setVisibility(View.VISIBLE); 
     pauseStopWatch.setVisibility(View.GONE); 
    } 
}); 

return rootView; 
} 
} 

这是布局文件请参阅此....

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/parentRelativeLayout" 
    android:layout_height="match_parent" 
    android:padding="@dimen/activity_horizontal_margin"> 

    <Chronometer 
     android:layout_centerInParent="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="30sp" 
     android:textAlignment="center" 
     android:id="@+id/stopwatch" 
     android:layout_margin="16dp"/> 

    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/stopwatch"> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/startStopWatch" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Start" 
       android:background="@drawable/buttonshape" 
       android:textSize="24sp" /> 

      <Button 
       android:id="@+id/pauseStopWatch" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Pause" 
       android:background="@drawable/buttonshape" 
       android:textSize="24sp" /> 

      <Button 
       android:id="@+id/stopStopWatch" 
       android:layout_marginTop="16dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/startStopWatch" 
       android:background="@drawable/buttonshape" 
       android:text="Stop" 
       android:textSize="24sp"/> 

      <Button 
       android:id="@+id/resetStopWatch" 
       android:layout_alignParentRight="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/buttonshape" 
       android:text="Reset" 
       android:textSize="24sp" /> 

      <Button 
       android:id="@+id/resumeStopWatch" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/buttonshape" 
       android:text="Resume" 
       android:textSize="24sp" 
       android:layout_marginTop="16dp" 
       android:layout_alignParentRight="true" 
       android:layout_below="@id/resetStopWatch"/> 


     </RelativeLayout> 

    </ScrollView> 


</RelativeLayout> 

This is the way buttons work when click on start button

+0

发布您的布局xml文件 – arjun

+0

发布布局文件...请现在回答... –

回答

0

您正在隐藏该按钮,但您仍在使用OnClickListener进行侦听。尝试添加:

pauseStopWatch.setOnClickListener(null); 

然后,当你让你的按钮可见:

pauseStopWatch.setOnClickListener(whatever); 
+0

请在查看布局文件和附加的图像后再回答... –

0

试试这个布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:id="@+id/parentRelativeLayout" 
android:layout_height="match_parent" 
android:padding="@dimen/activity_horizontal_margin"> 

<Chronometer 
    android:layout_centerInParent="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:textAlignment="center" 
    android:id="@+id/stopwatch" 
    android:layout_margin="16dp"/> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/stopwatch"> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
    <LinearLayout 
     android:gravity="center" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 
     <Button 
      android:id="@+id/startStopWatch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Start" 
      android:background="@drawable/buttonshape" 
      android:textSize="24sp" /> 

     <Button 
      android:visibility="gone" 
      android:id="@+id/pauseStopWatch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Pause" 
      android:background="@drawable/buttonshape" 
      android:textSize="24sp" /> 

     <Button 
      android:id="@+id/stopStopWatch" 
      android:layout_marginTop="16dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/startStopWatch" 
      android:background="@drawable/buttonshape" 
      android:text="Stop" 
      android:textSize="24sp"/> 
    </LinearLayout> 
    <LinearLayout 
     android:gravity="center" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 
     <Button 
      android:id="@+id/resetStopWatch" 
      android:layout_alignParentRight="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/buttonshape" 
      android:text="Reset" 
      android:textSize="24sp" /> 

     <Button 
      android:id="@+id/resumeStopWatch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/buttonshape" 
      android:text="Resume" 
      android:textSize="24sp" 
      android:layout_marginTop="16dp" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/resetStopWatch"/> 


    </LinearLayout> 
</LinearLayout> 

在上面,在pause按钮将被设置为Gone。用户点击后可以看到start按钮