我有以下代码,我想要做的是当单击sigup textview时它应该显示第二个布局,即相对布局之上的linearlayout。它正在努力到这一步。接下来,我想在第二个屏幕上单击按钮允许时显示另一个带有注册详细信息的布局,但是没有发生。附加了xml文件。
package care.e_eyepewsversion2;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button login = (Button) findViewById(R.id.loginbutton);
TextView signup= (TextView) findViewById(R.id.signup);
EditText email1 = (EditText) findViewById(R.id.Email);
EditText password1 = (EditText) findViewById(R.id.Password);
if (signup != null) {
signup.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View V){
Button login = (Button) findViewById(R.id.loginbutton);
login.setVisibility(View.INVISIBLE);
LinearLayout adminacesslayout= (LinearLayout) findViewById(R.id.adminaccesslayout) ;
int visibility = adminacesslayout.getVisibility();
if(visibility==View.GONE)
{adminacesslayout.setVisibility(View.VISIBLE);}
}
}
);
}
LinearLayout adminacesslayout= (LinearLayout) findViewById(R.id.adminaccesslayout) ;
int visibility1=adminacesslayout.getVisibility();
if(visibility1==View.VISIBLE)
{
Button adminlogin = (Button) findViewById(R.id.adminpassbutton);
if (adminlogin != null) {
adminlogin.setOnClickListener(new Button.OnClickListener(){
public void onClick(View V){
EditText adminpass= (EditText) findViewById(R.id.admiinpass);
String adminpasswordtyped= adminpass.getText().toString();
// if("1234".equals(adminpasswordtyped))
// { LinearLayout adminacesslayout= (LinearLayout) findViewById(R.id.adminaccesslayout) ;
// adminacesslayout.setVisibility(View.GONE);
// }
RelativeLayout signuplayout= (RelativeLayout) findViewById(R.id.signup_layout);
int visibility2=signuplayout.getVisibility();
if(visibility2==View.GONE)
{
LinearLayout adminacesslayout= (LinearLayout) findViewById(R.id.adminaccesslayout) ;
adminacesslayout.setVisibility(View.GONE);
signuplayout.setVisibility(View.VISIBLE);
}
}
}
);
}
}
login.setOnClickListener(new Button.OnClickListener() {
public void onClick(View V) {
checkdetails();
}
}
);
}
public void checkdetails() {
EditText email = (EditText) findViewById(R.id.Email);
EditText password = (EditText) findViewById(R.id.Password);
String emailtyped = email.getText().toString();
String passwordtyped = password.getText().toString();
String filename = "my file";
//Writing to file section
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(emailtyped.getBytes());
outputStream.write("\r\n".getBytes());
outputStream.write(passwordtyped.getBytes());
outputStream.close();
Context context = getApplicationContext();
CharSequence text = "Login history Saved";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} catch (Exception e) {
e.printStackTrace();
}
//Reading from file section Text
if (getString(R.string.firstemailtyped).equals(emailtyped)) {
if ("1234".equals(passwordtyped)) {
Button loogin = (Button) findViewById(R.id.loginbutton);
TextView dummydata= (TextView) findViewById(R.id.dummydata);
// After that, you can call read method to read one character at a time from the file and then you can print it. Its syntax is given below −
try {
InputStream inputStream = openFileInput("my file");
if (inputStream != null) {
InputStreamReader streamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String l;
String k="";
while (( l = bufferedReader.readLine()) != null) {
// do what you want with the line
k=(k+l);
dummydata.setText(k);
}
}
inputStream.close(); //close the file
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<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="care.e_eyepewsversion2.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/log_in"
android:id="@+id/loginbutton"
android:layout_marginBottom="166dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="@color/login_but_text_clr"
android:background="@color/login_button_clr"
android:visibility="visible"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentEnd="true"
android:src="@drawable/carelogo"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/Email"
android:layout_above="@+id/loginbutton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="110dp"
android:visibility="visible"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/Password"
android:layout_above="@+id/loginbutton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="E-EYE Platoon Early Warning System"
android:textAlignment="center"
android:textColor="@color/Email_password_text"
android:textAllCaps="true"
android:textStyle="bold"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Email"
android:id="@+id/textView2"
android:layout_marginRight="30dp"
android:textSize="15sp"
android:textColor="@color/Email_password_text"
android:layout_alignBottom="@+id/Email"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Password"
android:textSize="15dp"
android:textColor="@color/Email_password_text"
android:id="@+id/textView3"
android:layout_alignBottom="@+id/Password"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/dummydata"
android:layout_above="@+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text= "Sign Up?"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="@color/login_button_clr"
android:textColorHint="@color/Email_password_text"
android:id="@+id/signup"
android:layout_alignBottom="@+id/loginbutton"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/loginbutton"
android:background="#539c65"
android:id="@+id/adminaccesslayout"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="ADMINISTRATOR PERMISSION REQUIRED"
android:textColor="#F00000"
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="center"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/admiinpass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#70FFFFFF"
android:text="ALLOW"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="center_horizontal"
android:id="@+id/adminpassbutton" />
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00bfff"
android:id="@+id/signup_layout"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup Details"
android:textAllCaps="true"
android:textSize="30sp"
android:textColor="#006400"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_toStartOf="@+id/newusername"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:layout_alignBottom="@+id/newusername" />
<EditText
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/newusername"
android:layout_marginTop="50dp"
android:textAllCaps="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_toStartOf="@+id/newemail"
android:textSize="20sp"
android:layout_alignBottom="@+id/newemail" />
<EditText
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/newemail"
android:layout_below="@id/newusername"
android:layout_marginTop="50dp"
android:textAllCaps="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_toStartOf="@+id/newpassword"
android:textSize="18sp"
android:layout_alignBottom="@+id/newpassword" />
<EditText
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/newpassword"
android:layout_below="@id/newemail"
android:layout_marginTop="50dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/newpassword"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:background="#8050"
android:text="SIGN UP"
android:textColor="#FFFFFF"
android:id="@+id/signupbutton"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
onCreate方法。
因此,当它在活动创建期间执行时,它会发现if(visibility1==View.VISIBLE)
不正确(因为adminacesslayout
有visibility GONE
)。
通过这种方式,您不会让adminlogin
膨胀,也不会在其上设置任何Button.OnClickListener
。
你需要的是:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button login = (Button) findViewById(R.id.loginbutton);
TextView signup = (TextView) findViewById(R.id.signup);
EditText email1 = (EditText) findViewById(R.id.Email);
EditText password1 = (EditText) findViewById(R.id.Password);
if (signup != null) {
signup.setOnClickListener(new TextView.OnClickListener() {
public void onClick(View V) {
showAdminacesslayout();
}
}
);
}
LinearLayout adminacesslayout = (LinearLayout) findViewById(R.id.adminaccesslayout);
int visibility1 = adminacesslayout.getVisibility(); // if(visibility1==View.VISIBLE) // { // // Button
adminlogin = (Button) findViewById(R.id.adminpassbutton); // //
if (adminlogin != null) { //
adminlogin.setOnClickListener(new Button.OnClickListener(){ //
public void onClick(View V){ //
EditText adminpass= (EditText) findViewById(R.id.admiinpass); //
String adminpasswordtyped= adminpass.getText().toString(); // //
// if("1234".equals(adminpasswordtyped)) //
// { LinearLayout adminacesslayout= (LinearLayout)
findViewById(R.id.adminaccesslayout) ; //
// adminacesslayout.setVisibility(View.GONE); //
// } //
RelativeLayout signuplayout= (RelativeLayout)
findViewById(R.id.signup_layout); //
int visibility2=signuplayout.getVisibility(); //
if(visibility2==View.GONE) //
{ //
LinearLayout adminacesslayout= (LinearLayout)
findViewById(R.id.adminaccesslayout) ; //
adminacesslayout.setVisibility(View.GONE); //
signuplayout.setVisibility(View.VISIBLE); //
} // // // } // //
} // // ); // } // }
login.setOnClickListener(new Button.OnClickListener() {
public void onClick(View V) {
checkdetails();
}
}
);
}
private void showAdminacesslayout() {
Button login = (Button) findViewById(R.id.loginbutton);
login.setVisibility(View.INVISIBLE);
LinearLayout adminacesslayout = (LinearLayout) findViewById(R.id.adminaccesslayout);
int visibility = adminacesslayout.getVisibility();
if (visibility == View.GONE) {
adminacesslayout.setVisibility(View.VISIBLE);
}
Button adminlogin = (Button) findViewById(R.id.adminpassbutton);
if (adminlogin != null) {
adminlogin.setOnClickListener(new Button.OnClickListener() {
public void onClick(View V) {
EditText adminpass = (EditText) findViewById(R.id.admiinpass);
String adminpasswordtyped = adminpass.getText().toString();
// if("1234".equals(adminpasswordtyped))
// { LinearLayout adminacesslayout= (LinearLayout)
findViewById(R.id.adminaccesslayout) ;
// adminacesslayout.setVisibility(View.GONE);
// }
RelativeLayout signuplayout = (RelativeLayout) findViewById(R.id.signup_layout);
int visibility2 = signuplayout.getVisibility();
if (visibility2 == View.GONE) {
LinearLayout adminacesslayout = (LinearLayout)
findViewById(R.id.adminaccesslayout);
adminacesslayout.setVisibility(View.GONE);
signuplayout.setVisibility(View.VISIBLE);
}
}
}
);
}
}
public void checkdetails() {
EditText email = (EditText) findViewById(R.id.Email);
EditText password = (EditText) findViewById(R.id.Password);
String emailtyped = email.getText().toString();
String passwordtyped = password.getText().toString();
String filename = "my file";
//Writing to file section
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(emailtyped.getBytes());
outputStream.write("\r\n".getBytes());
outputStream.write(passwordtyped.getBytes());
outputStream.close();
Context context = getApplicationContext();
CharSequence text = "Login history Saved";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} catch (Exception e) {
e.printStackTrace();
}
//Reading from file section Text
if ("ciao".equals(emailtyped)) { // if (getString(R.string.firstemailtyped).equals(emailtyped)) {
if ("1234".equals(passwordtyped)) {
Button loogin = (Button) findViewById(R.id.loginbutton);
TextView dummydata = (TextView) findViewById(R.id.dummydata);
// After that, you can call read method to read one character at a time from the file and then you can print it. Its
syntax is given below −
try {
InputStream inputStream = openFileInput("my file");
if (inputStream != null) {
InputStreamReader streamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String l;
String k = "";
while ((l = bufferedReader.readLine()) != null) {
// do what you want with the line
k = (k + l);
dummydata.setText(k);
}
}
inputStream.close(); //close the file
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
或者更简单地说,您只能删除这些行,并将代码保留在括号中:
int visibility1 = adminacesslayout.getVisibility();
if(visibility1==View.VISIBLE)
{
...
}