【问题标题】:My app button won't work我的应用按钮不起作用
【发布时间】:2015-11-29 14:48:57
【问题描述】:

我正在尝试创建一个应用程序,该应用程序在按下按钮时会更改显示的文本。但是它不起作用。我检查了我的代码,我没有发现我的事件处理程序有任何问题。这是我的代码:

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>

【问题讨论】:

  • 你得到什么错误? (如果有)
  • 使用getResources().getString(R.string.NewMassage) 而不是getString(R.string.NewMassage)
  • 从您的布局 xml 文件中的 &lt;Button&gt; 中删除 android:enabled="false"

标签: java android android-button


【解决方案1】:

改变这一行

displayText.setText(R.string.NewMassage);

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

R.string.NewMassageInteger 的形式返回NewMassage 字符串资源的id。

正如activesince93 所说,在按钮 OnclickListener 外声明您的 textView

【讨论】:

    【解决方案2】:

    替换这个,

    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);
    

    注意:确保你在strings.xml中声明了NewMassage

    <string name="NewMassage">Text you want to display</string>
    

    【讨论】:

    • 我按照您的建议替换了代码,并在“onClick”之外声明了“TextView”,但它仍然不起作用。我不知道还能做什么......
    • 也尝试替换 displayText
    • 用什么?或者我该如何替换它
    • 更新您的代码。这就是我要说的。 displayText.setText(R.string.NewMassage); 是错误的。删除该行。并添加displayText.setText(getString(R.string.NewMassage));
    • 是的,我已经按照您之前的建议这样做了...用 displayText.setText(getString(R.string.NewMassage) ); 替换了“displayText”行但我的按钮仍然无法使用。
    猜你喜欢
    • 2016-05-25
    • 2020-10-21
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多