Android按钮:获取文本

时间:2013-10-02 17:41:36

标签: java android button gettext

我会尽可能快地回顾这个问题。我正在开发一个需要通过按钮文本进行复制的应用程序。让我以代码方式解释它。如果我的按钮有“此文本”,那么就这样做。过去两天我一直在尝试几种不同的事情,而且我已经走到了尽头,所以我在问。这是我的代码:

XML:

<Button
                android:id="@+id/bHiddenL1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                android:layout_margin="18dp"
                android:text="@string/NotUsed"
                android:maxLength="10" />

Java:

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.newnotebookbutton);
            initializeNotebookNewSubject();


        }


            public void initializeNotebookNewSubject() {

                NewTextInput = (EditText) findViewById(R.id.etNewNotebookButtonCreateSubjectButton);
                OKButton = (Button) findViewById(R.id.bOkButton);
                Button1L = (Button) findViewById(R.id.bHiddenL1);           
                Button2L = (Button) findViewById(R.id.bHiddenL2);           
                Button3L = (Button) findViewById(R.id.bHiddenL3);           
                Button4L = (Button) findViewById(R.id.bHiddenL4);           
                Button5L = (Button) findViewById(R.id.bHiddenL5);           
                Button1R = (Button) findViewById(R.id.bHiddenR1);           
                Button2R = (Button) findViewById(R.id.bHiddenR2);           
                Button3R = (Button) findViewById(R.id.bHiddenR3);           
                Button4R = (Button) findViewById(R.id.bHiddenR4);           
                Button5R = (Button) findViewById(R.id.bHiddenR5);           

                OKButton.setOnClickListener(this);

            }


            @Override
            public void onClick(View v) {

                String b1L = Button1L.getText().toString();
                String b2L = Button2L.getText().toString();
                String b3L = Button3L.getText().toString();
                String b4L = Button4L.getText().toString();
                String b5L = Button5L.getText().toString();
                String b1R = Button1R.getText().toString();
                String b2R = Button2R.getText().toString();
                String b3R = Button3R.getText().toString();
                String b4R = Button4R.getText().toString();
                String b5R = Button5R.getText().toString();

                if(b1L == "Not Used" && NewTextInput.getText().toString() != null){

                    NewSubjectBundle.putString("title1L", NewTextInput.getText().toString());
                    NewSubjectBundle.putInt("int", 1);
                     mIntent.putExtras(NewSubjectBundle);
                     setResult(RESULT_OK, mIntent);
                     finish();

让我更好地指出我的问题。每当我试图做“String b1L = Button1L.getText()。toString();”时,我在logcat中得到一个错误,指出指针是空的。该如何解决?

2 个答案:

答案 0 :(得分:1)

请勿使用==来比较字符串的内容。

b1L == "Not Used"

应该是

b1L.equals("Not Used")

甚至更好

"Not Used".equals(b1L)

How do I compare strings in Java ?

答案 1 :(得分:0)

邹邹是对的。但在你的情况下,更好的是:

b1L.equals(getResources().getString(R.string.NotUsed));