如何在Android中强制执行条款和条件协议?我想这样做http://www.droidbin.com/。您可以在给定的链接中观察到只有在勾选复选框时才会显示上传,否则它将不可见,并且最初它是不可见的。
我的登录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="0123456789"
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");
}
}
});
}
答案 0 :(得分:1)
您需要使用CheckBox。只需使用XML标记<CheckBox/>
在您的布局的.xml文件中添加复选框。
然后将OnCheckedChangeListener添加到CheckBox:
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);
}
});
答案 1 :(得分: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();
}
});
}