比较文本字段值
Comparing Text Fields Value
使用 Internet、Android Studio 以及您当前对 Android 应用程序开发的了解,创建一个 Android 应用程序项目,其中包含两 (2) 个文本字段和一 (1) ) 按钮。该按钮将比较来自文本字段的输入并在单击时显示响应(如果值相同则为 SAME,否则为 NOT THE SAME)。您可能需要为此
创建一个新的 activity
在使用之前学习基础知识非常重要 Whosebug 不像其他 from scratch 教程站点,您需要付出一些努力首先.. 如果有一天你像我们一样是初学者 are/were,Google 是你最好的朋友。
然而,正如您所要求的...
首先我们需要创建两个 activities
一个用于输入,另一个用于显示结果,当然你可以在 activity.
中创建
第一个Activity
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="value 1"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="value 2"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="compare"
android:text="Compare" />
</LinearLayout>
关联.java
文件:根据需要更改包名
package com.app.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void compare(View view) {
// get the values form those two text views
String value1 = ((EditText) findViewById(R.id.editText1)).getText().toString(); // you can use global variable too.. here, I've just used the shortcut
String value2 = ((EditText) findViewById(R.id.editText2)).getText().toString();
Intent intent = new Intent(this, ResultActivity.class);
if (value1.equals(value2)) {
// same
intent.putExtra("result", true);
} else {
intent.putExtra("result", false);
}
startActivity(intent);
}
}
现在 ResultActivity
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ResultActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
</LinearLayout>
关联class:.java:
package com.app.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
boolean result = getIntent().getBooleanExtra("result", false);
if (result)
((TextView) findViewById(R.id.textView)).setText("SAME");
else
((TextView) findViewById(R.id.textView)).setText("NOT SAME");
}
}
使用 Internet、Android Studio 以及您当前对 Android 应用程序开发的了解,创建一个 Android 应用程序项目,其中包含两 (2) 个文本字段和一 (1) ) 按钮。该按钮将比较来自文本字段的输入并在单击时显示响应(如果值相同则为 SAME,否则为 NOT THE SAME)。您可能需要为此
创建一个新的 activity在使用之前学习基础知识非常重要 Whosebug 不像其他 from scratch 教程站点,您需要付出一些努力首先.. 如果有一天你像我们一样是初学者 are/were,Google 是你最好的朋友。 然而,正如您所要求的...
首先我们需要创建两个 activities
一个用于输入,另一个用于显示结果,当然你可以在 activity.
第一个Activity
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="value 1"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="value 2"
android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="compare"
android:text="Compare" />
</LinearLayout>
关联.java
文件:根据需要更改包名
package com.app.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void compare(View view) {
// get the values form those two text views
String value1 = ((EditText) findViewById(R.id.editText1)).getText().toString(); // you can use global variable too.. here, I've just used the shortcut
String value2 = ((EditText) findViewById(R.id.editText2)).getText().toString();
Intent intent = new Intent(this, ResultActivity.class);
if (value1.equals(value2)) {
// same
intent.putExtra("result", true);
} else {
intent.putExtra("result", false);
}
startActivity(intent);
}
}
现在 ResultActivity
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ResultActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Display2" />
</LinearLayout>
关联class:.java:
package com.app.myapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
boolean result = getIntent().getBooleanExtra("result", false);
if (result)
((TextView) findViewById(R.id.textView)).setText("SAME");
else
((TextView) findViewById(R.id.textView)).setText("NOT SAME");
}
}