0
   package walmart.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

android.widget.EditText をインポートします。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を初めて使用するので、ディスプレイに何も表示されない理由がわかりません。

edit現在持っているものに合わせてコードを編集しました。まだ動作していません。

4

1 に答える 1

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 を付けて (たとえば、display としましょう) 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>
于 2012-05-01T00:08:03.927 に答える