私はアンドロイドとJavaが初めてです。私は助けが必要です。以下に示すように、メソッド isValid に String を使用する必要があります。ただし、文字列には演算子 > と < を使用できません。ユーザーが9桁以上の無効な番号を入力できないようにするために使用しています。前もって感謝します。
package com.Elson.ProjectVersion;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
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 EnterContactsActivity extends Activity {
private Button saveButton;
private EditText NameEditText;
private EditText PhoneEditText;
private Button ExitButton;
private EditText EmailEditText;
private TextView date;
private int month;//private within class
private int day;
private int year;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontacts);
setUpViews();
Calendar calendar =Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
Date today = calendar.getTime();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
String cs = df.format(today);
date.setText(cs);
}
public void saveClickHandler(View v){
String ContactsScore;
ContactsScore= NameEditText.getText().toString();
String name = String.format(ContactsScore);
ContactsScore= PhoneEditText.getText().toString();
String Phone = String.format(ContactsScore);
Log.d("EnterContacts" , "I hear the Save Button");
if ( isValid(Phone) ){
Contacts contacts;
Date dateofGames= new GregorianCalendar(year,month,day).getTime();
contacts = new Contacts (name , Phone , dateofGames);
ContactsActivityApplication app = (ContactsActivityApplication) getApplication();
//might be wrong
Log.d("DeBUGGING", "app is this type: " + app.getClass().getName());
//need add the function addBowlingScores
app.addallContacts(contacts);
Toast.makeText(getApplicationContext(), "Your Contact has been Saved!", Toast.LENGTH_SHORT).show();
}
if( name == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please enter your name")
.setMessage("Phone numbers cannot have more than 8 numbers")
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
//error is here
private boolean isValid() {
if (Phone > 0 && Phone < 100000000)
return true;
return false;
// TODO Auto-generated method stub
}
private void setUpViews()
{
ExitButton = (Button) findViewById(R.id.BtnExit);
saveButton =(Button) findViewById(R.id.BtnSave);
NameEditText= (EditText) findViewById(R.id.NameEditText);
PhoneEditText= (EditText) findViewById(R.id.PhoneEditText);
EmailEditText= (EditText) findViewById(R.id.EmailEditText);
date = (TextView) findViewById(R.id.DateTextView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.addcontacts, menu);
return true;
}
}
連絡先
package com.Elson.ProjectVersion;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
public class Contacts implements Comparable<Contacts> {
private long id;
private String name;
private String Phone;
private int Email;
private Date date;
private double runningAverage;
public Contacts(String name, String Phone, Date date) {
this.name = name;
this.Phone = Phone;
this.date = null;
}
public Contacts(long id, String name,String Phone) {
this.id=id;
this.Phone=Phone;
this.name= (name);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPhone() {
return Phone;
}
public void setPhone(String Phone) {
this.Phone = Phone;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public Date getDate() {
return null;
}
public long getDateEpoch(){
return date.getTime()/1000;
}
public void setDateEpoch(long seconds){
date= new Date (seconds*1000);
}
public void setDate(Date date) {
this.date = date;
}
public void setRunningAverage(double runningAverage) {
this.runningAverage = runningAverage;
}
public boolean equals(Object that){
Contacts bs = (Contacts) that;
return this.date.equals(bs.date);
}
@Override
public String toString() {
String result;
if(date != null) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
result = df.format(date) + "" + name + "" + Phone ;
}
else {
result = name + "" + Phone ;
}
return result;
}
@Override
public int compareTo(Contacts another) {
// TODO Auto-generated method stub
return 0;
}
}