我的应用按钮无法正常工作

时间:2015-11-29 14:48:57

标签: java android android-button

我尝试创建一个应用程序,只需按一下按钮,即可更改显示的文本。然而,它不起作用。我检查了我的代码,我没有看到我的事件处理程序有什么问题。这是我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       //Reference Button
        Button ekeButton = (Button)findViewById(R.id.ekeButton);

        ekeButton.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View view) {
                        TextView displayText = (TextView) findViewById(R.id.displayText);
                        displayText.setText(R.string.NewMassage);

                    }
                }
        );

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我更新的代码:

    package com.mesoft.eventhandler;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       //Reference Button
        Button ekeButton = (Button)findViewById(R.id.ekeButton);
        final TextView displayText = (TextView) findViewById(R.id.displayText);


        ekeButton.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View view) {
                        displayText.setText(getString(R.string.NewMassage));

                    }
                }
        );



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我在string.xml中声明了我的字符串 这是代码:

    <resources>
    <string name="app_name">Event Handler</string>
    <string name="action_settings">Settings</string>
    <string name="click">Choose Massage</string>
    <string name="ShowMassage">Merry Xmas</string>
    <string name="NewMassage">Happy new year</string>
</resources>

这里是activity_main.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"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/ShowMassage"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:id="@+id/displayText" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click"
        android:id="@+id/ekeButton"
        android:layout_marginTop="74dp"
        android:enabled="false"
        android:layout_below="@+id/displayText"
        android:layout_alignLeft="@+id/displayText"
        android:layout_alignStart="@+id/displayText"
        android:textColor="#090909" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

替换它,

displayText.setText(R.string.NewMassage);

displayText.setText(getString(R.string.NewMassage));

而且,

最好宣布

TextView displayText = (TextView) findViewById(R.id.displayText);

onClick()方法。我的意思是下面

Button ekeButton = (Button)findViewById(R.id.ekeButton);
  

注意:确保您已在NewMassage

中声明strings.xml
<string name="NewMassage">Text you want to display</string>

答案 1 :(得分:0)

更改此行

$(e.target).closest("tr.jqgrow").attr("id")

displayText.setText(R.string.NewMassage);

displayText.setText(getResources().getString(R.string.NewMassage)); R.string.NewMassage的形式返回NewMassage字符串资源的ID。

同样如activesince93所述,声明你的textView外部按钮OnclickListener