如何编写适当的代码到按钮链接到应用程序而不是布局 [英] how to write the appropriate code to button to link to application not the layout
问题描述
我正在开发一个小型基本程序,通过登录形式的前缀显示计算器,其中我有以下代码。我有三个.java文件
1.Mainactivity .java
2.Signinactivity.java
3.Signupactivity.java
1.here是Maainactivity.java的内容
i am working on a small basic program to display a calculator through in prefix of log in form in which i am having the following code.here i am having three .java files
1.Mainactivity.java
2.Signinactivity.java
3.Signupactivity.java
1.here is content of Maainactivity.java
package com.hmkcode.android.sign;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button btnSignIn;
Button btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = (Button) findViewById(R.id.btnSingIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignIn.setOnClickListener(this);
btnSignUp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i = null;
switch(v.getId()){
case R.id.btnSingIn:
i = new Intent(this,SignInActivity.class);
break;
case R.id.btnSignUp:
i = new Intent(this,SignUpActivity.class);
break;
}
startActivity(i);
}
}
2.其中包含Signinactivity.java的内容 < br $>
2.here is content of Signinactivity.java
package com.hmkcode.android.sign;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignInActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in_screen);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")){
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3。这里是Signupactivity.java的内容
3.here is content of Signupactivity.java
package com.hmkcode.android.sign;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class SignUpActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_screen);
}
}
三种XML布局,即res / layout
1.activity_main.xml
2.activity_sign_in_screen.xml
3.activity_sign_up_screen.xml
1.其中包含activity_main.xml的内容
And the three XML layouts namely in res/layout
1.activity_main.xml
2.activity_sign_in_screen.xml
3.activity_sign_up_screen.xml
1.here is content of activity_main.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: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=".MainActivity"
android:removed="@drawable/balloon_bg">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:removed="@drawable/linearlayout_bg"
android:padding="10dp"
>
<Button
android:id="@+id/btnSignUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="Sign Up"
android:background="@drawable/button_default_bg"
style="@style/DefaultButtonText"
/>
<Button
android:id="@+id/btnSingIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="Sign In"
style="@style/DefaultButtonText"
android:background="@drawable/button_default_bg"
/>
</LinearLayout>
</RelativeLayout>
1.其中包含activity_sign_in_screen.xml的内容
1.here is content of activity_sign_in_screen.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: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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="50dp"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="38dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginLeft="30dp"
android:layout_marginTop="48dp"
android:text="@string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_alignTop="@+id/textView4"
android:text="TextView" />
</RelativeLayout>
3.here is content of activity_sign_up_screen.xml
3.here is content of activity_sign_up_screen.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: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=".MainActivity"
android:removed="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
>
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:removed="@drawable/edittext_top_bg"
android:padding="10dp"
android:hint="Email"
android:textColorHint="#bbbbbb"
android:drawableLeft="@drawable/email"/>
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:removed="@drawable/edittext_default_bg"
android:layout_marginTop="-2dp"
android:padding="10dp"
android:hint="User Name"
android:textColorHint="#bbbbbb"
android:drawableLeft="@drawable/user"/>
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:removed="@drawable/edittext_bottom_bg"
android:layout_marginTop="-2dp"
android:padding="10dp"
android:hint="Password"
android:textColorHint="#bbbbbb"
android:password="true"
android:drawableLeft="@drawable/password"/>
<Button
android:id="@+id/btnSingIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="Sign Up"
style="@style/DefaultButtonText"
android:background="@drawable/button_default_bg"
/>
</LinearLayout>
</RelativeLayout>
And also here is c ontent of res/values/string.xml
And also here is content of res/values/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sign In&Up</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="username">Username:</string>
<string name="password">Password:</string>
<string name="Login">Login:</string>
<string name="attempts">Attempts Left:</string>
<style name="DefaultButtonText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#979797</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">3dp</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">20sp</item>
<item name="android:shadowColor">#f9f9f9</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
</style>
</resources>
And the content of Androidmanifest.xml is
And the content of Androidmanifest.xml is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hmkcode.android.sign"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hmkcode.android.sign.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.hmkcode.android.sign.SignUpActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.SignUpActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.hmkcode.android.sign.SignInActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.SignInActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Iam getting the output as it is written in code of activity_main.xml,when i am pressing the sign in button then it goes to next layout of activity_sign_up_screen.xml
then login screen appears ,then when i click the login button it is diplayin redirecting according to the given code but the main problem is i need to keep the simple calculator project which i go from here
As iam totally new and dont have knowledge of android and also java,would anyone please help me ..
Thanks in advance
Iam getting the output as it is written in code of activity_main.xml,when i am pressing the sign in button then it goes to next layout of activity_sign_up_screen.xml
then login screen appears ,then when i click the login button it is diplayin redirecting according to the given code but the main problem is i need to keep the simple calculator project which i go from here
As iam totally new and dont have knowledge of android and also java,would anyone please help me ..
Thanks in advance
推荐答案
I would suggest you go to http://download.oracle.com/javase/tutorial/index.html[^] to learn Java. Then take a look at some of the excellent tutorials on Android at http://www.codeproject.com/KB/android/#Android+Tutorial+Contest[^]. You also need to spend time learning how to debug or diagnose problems in your code and just post the relevant parts of your program. Few people here have time to go through something as long as you have posted.
I would suggest you go to http://download.oracle.com/javase/tutorial/index.html[^] to learn Java. Then take a look at some of the excellent tutorials on Android at http://www.codeproject.com/KB/android/#Android+Tutorial+Contest[^]. You also need to spend time learning how to debug or diagnose problems in your code and just post the relevant parts of your program. Few people here have time to go through something as long as you have posted.
这篇关于如何编写适当的代码到按钮链接到应用程序而不是布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!