I’m new to programming for the android and I’m trying to create a simple program.
If the user enters a number in the attacker ws field and the same number defender ws field you should get an answer.
I’m missing something.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroid extends Activity implements OnClickListener
{
private EditText text, text2, text3;
private Button btutorial1;
int result = text.toString().compareTo(text2.toString());
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("I hope this works");
text = (EditText) findViewById(R.id.editText1);
text2 =(EditText) findViewById(R.id.editText2);
text3 = (EditText) findViewById(R.id.editText3);
btutorial1 = (Button) findViewById(R.id.button1);
btutorial1.setOnClickListener(this);
}
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button1:
if (text.getText().toString().equals(1) && text2.getText().toString().equals(2))
{
text3.setText("Five and above");
return;
}
else if (text.getText().toString().equals(1) && text2.getText().toString().equals(3))
{
text3.setText("Five and above");
return;
}
else if (text.getText().toString().equals(1) && text2.getText().toString().equals(3))
{
text3.setText("Four and above");
return;
}
else if (text.getText().toString().equals(text2.getText().toString()))
{
text3.setText("Four and above");
return;
}
else
{
text3.setText("Not Working");
return;
}
}
}
}
My XML class named main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Attacker ws"></TextView>
<EditText android:id="@+id/editText1" android:text="" android:layout_height="wrap_content" android:layout_width="match_parent" android:inputType="number" android:visibility="visible"></EditText>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Opponent ws"></TextView>
<EditText android:id="@+id/editText2" android:text="" android:layout_height="wrap_content" android:layout_width="match_parent" android:visibility="visible" android:inputType="number"></EditText>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" android:visibility="visible" android:onClick="myClickHandler"></Button>
<EditText android:id="@+id/editText3" android:layout_height="wrap_content" android:layout_width="match_parent" android:name="result" android:editable="false"></EditText>
</LinearLayout>
If you are comparing two integers then do this.
now in if condition compare number1 and number2.
Remember this will work only for integers. for any string you will get exception you can handle it later on.