2016-11-03 42 views
-2

如何在Android中強制執行條款和條件協議?我想這樣做http://www.droidbin.com/。您可以在給定的鏈接中觀察到,只有當複選框被勾選時,上載纔會出現,否則它將不可見並且最初不可見。執行條款和條件在Android協議?

我登錄XML:

<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:background="@drawable/startbg" 
       android:padding="0dip" 
       tools:context=".LoginActivity"> 

    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:orientation="vertical" 
      android:paddingLeft="40dp" 
      android:paddingRight="40dp"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="40dp" 
       android:text="Enter your number" 
       android:textAllCaps="true" 
       android:id="@+id/textView" android:layout_gravity="center_horizontal" 
       android:textColor="@color/sinch_purple" android:textSize="20sp"/> 
     <EditText 
       android:id="@+id/loginNo" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="8dp" 
       android:inputType="phone" 
       android:digits="" 
       android:maxLength="10" 
       android:textSize="32sp" 
       android:padding="10dp" 
       android:textColor="@color/sinch_purple" 
       android:background="@drawable/inputbox"> 


      <requestFocus/> 
     </EditText> 

    </LinearLayout> 

    <Button 
      android:id="@+id/loginButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="40dp" 
      android:layout_gravity="center_horizontal" 
      android:background="@drawable/button_login" 
      android:minHeight="56dp" 
      android:minWidth="132dp" 
      android:textColor="@color/off_white" 
      android:textSize="22sp" 
      android:visibility="invisible" 
      android:textStyle="bold" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> 
    <TextView 
     android:id="@+id/terms" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/sinch_purple" 
     android:layout_toRightOf="@id/checkBox1" 
     android:layout_alignTop="@id/checkBox1" 
     android:textSize="16sp"/> 

</RelativeLayout> 

登錄的Java文件

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mBox = (CheckBox)findViewById(R.id.checkBox1); 
     setContentView(R.layout.login); 
     String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>"; 

     mBox.setText(Html.fromHtml(checkBoxText)); 
     mBox.setMovementMethod(LinkMovementMethod.getInstance()); 

     //terms=(TextView)findViewById(R.id.terms); 
     mLoginNo = (EditText) findViewById(R.id.loginNo); 

     mLoginButton = (Button) findViewById(R.id.loginButton); 
     mLoginButton.setEnabled(false); 
     mLoginButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       loginClicked(); 
      } 
     }); 
     mBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
     { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       if (mBox.isChecked()) 
       { 
        mLoginButton.setVisibility(buttonView.VISIBLE); 
       } 
       else{ 
        mLoginButton.setVisibility(buttonView.GONE); 
        // terms.setText("Please Agree to Terms And Condition"); 
       } 
      } 
     }); 
    } 

回答

1

您需要使用一個複選框。只需使用XML標記<CheckBox/>將複選框添加到佈局的.xml文件中。

然後一個OnCheckedChangeListener添加到複選框:

CheckBox checkBox = null;//get the view using findViewById 
    final View invisibleView = null;//do the same 
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       invisibleView.setVisibility(View.VISIBLE); 
      } else 
       invisibleView.setVisibility(View.GONE); 
     } 
    }); 
+0

優秀的,但我已經做到了

+0

然後有什麼問題? –

+0

雖然我沒有使用CompundButton,你能告訴我它有什麼用處嗎? – SameerKhan1406

0

看看我做的XML:

<Button 
       android:id="@+id/loginButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="80dp" 
       android:layout_gravity="center_horizontal" 
       android:background="@drawable/button_login" 
       android:minHeight="56dp" 
       android:minWidth="132dp" 
       android:textColor="@color/off_white" 
       android:textSize="22sp" 
       android:visibility="gone" 
       android:textStyle="bold" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="15dp" 
      /> 

而且在Java中文件

private Button mLoginButton; 
    private EditText mLoginNo; 
    private ProgressDialog mSpinner; 
    private CheckBox mBox; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 
     mBox = (CheckBox) findViewById(R.id.checkBox1); 
     String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>"; 

     mBox.setText(Html.fromHtml(checkBoxText)); 
     mBox.setMovementMethod(LinkMovementMethod.getInstance()); 
     mLoginButton = (Button) findViewById(R.id.loginButton); 
     mBox.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(mBox.isChecked()){ 
        mLoginButton.setVisibility(View.VISIBLE); 
       } 
       else{ 
        mLoginButton.setVisibility(View.GONE); 
       } 
      } 
     }); 

     mLoginNo = (EditText) findViewById(R.id.loginNo); 


     mLoginButton.setEnabled(false); 
     mLoginButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       loginClicked(); 
      } 
     }); 
    }