我正在尝试开发一个简单的应用来查找给定日期的日
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.dayfinder.MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/bg" />
<LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Date:"
android:textColor="@android:color/black"
android:textSize="35dp" />
<TextView
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Month:"
android:textColor="@android:color/black"
android:textSize="35dp" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:fontFamily="sans-serif-condensed"
android:text="Year:"
android:textColor="@android:color/black"
android:textSize="35dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/editText1"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="38dp"
android:layout_weight="1"
android:background="@android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Date"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="40dp"
android:layout_weight="1"
android:background="@android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Month!"
android:inputType="date"
android:maxLength="2"
android:textAlignment="gravity"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="41dp"
android:layout_weight="1"
android:background="@android:color/white"
android:fontFamily="sans-serif-condensed"
android:hint="Enter Year!"
android:inputType="date"
android:maxLength="4"
android:textAlignment="gravity"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="20dp">
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_bel="@android:color/white"
android:onClick="chow="@id/l1"
android:layout_centerHorizontal="true"
android:text="Find"
android:layout_margin="24dp"
android:backgroundeck"/>
<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_margin="48dp"
android:fontFamily="sans-serif-condensed"
android:text="Answer"
android:textColor="@android:color/black"
android:textSize="40dp" />
</RelativeLayout>
MainActivity.java
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button mbutton=(Button)findViewById(R.id.button);
public void check(View view) {
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
dayfinder f=new dayfinder();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}
package dayfinder.java (我在mainactivity.java中使用)
package source;
public class dayfinder {
public String finder(String a1, String a2, String a3) {
int q, w, x;
String e1;
q = Integer.parseInt(a1);
if ((q < 1000) || (q > 4000)) {
e1 = "error";
return e1;
}
else
{
e1="null";
return e1;
}
}
}
答案 0 :(得分:0)
重新格式化您的 MainActivity.java ,如果您在此之后有任何错误,请更新您的问题
遵循命名惯例
将dayfinder
对象创建转移到onCreate
,以避免每次调用check
方法时都创建新对象
package com.example.android.dayfinder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import source.dayfinder;
public class MainActivity extends AppCompatActivity {
int e1;
String s1,s2,s3;
private EditText medit1;
private EditText medit2;
private EditText medit3;
private TextView mText ;
private Button mbutton;
private dayfinder f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
f=new dayfinder();
}
public void initializeViews(){
mbutton=(Button)findViewById(R.id.button);
medit1=(EditText)findViewById(R.id.editText1);
medit2=(EditText)findViewById(R.id.editText2);
medit3=(EditText)findViewById(R.id.editText3);
mText = (TextView)findViewById(R.id.answer);
}
public void check(View view) {
s1=medit1.getText().toString();
s2=medit2.getText().toString();
s3=medit3.getText().toString();
f.finder(s1,s2,s3);
mText.setText(f.finder(s1,s2,s3));
}
}