package walmart.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; import android.widget.TextView;
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
name = (EditText)findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.tvdisplay);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name.setText("");
}
});
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (name.equals("Electronics")) {
display.setText ('5');
} else if (name.equals("candy")) {
display.setText ('1');
} else if (name.equals("Tobacco")) {
display.setText ("1");
}
}});
};}
无论我做什么,我的输出中都没有显示任何内容。我对JAVA非常非常新,所以我似乎无法找出为什么我的显示器上没有显示任何内容。
编辑编辑我的代码到我现在拥有的代码。还是行不通。
答案 0 :(得分:1)
查看该代码,问题可能在于您如何初始化名称和搜索。
更改您的代码:
name = (EditText) findViewById(getResources().getIdentifier("etName", "id", getPackageName()));
search = (Button) findViewById(getResources().getIdentifier("btnSearch", "id", getPackageName()));
到此:
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
另外,添加TextView以在main.xml
文件中显示您的答案,给它一个ID(例如显示)android:id="@+id/display"
,然后将其添加到您的活动中,以便您可以更改它:
display = (TextView) findViewById(R.id.display);
然后你可以通过调用:
来更新它display.setText('some text');
因此,文件的开头应如下所示:
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.display);
....
接下来更新您的if
语句:
if (name.equals("Electronics"))
为:
if (name.getText().toString().equals("Electronics")) {
display.setText("something");
}
一段代码示例:
Demo2Activity.java
package com.demo1;
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 Demo2Activity extends Activity {
private Button button;
private EditText editText;
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textText);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (editText.getText().toString().equals("Electronics")) {
textView.setText("found");
}
}
});
}
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="main2.xml"
android:id="@+id/textText" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>