【问题标题】:OnClickListener doesn't fireOnClickListener 不会触发
【发布时间】:2015-08-13 13:18:34
【问题描述】:

问题

单击按钮时不会触发此代码。

mBtnSend = (Button) findViewById(R.id.btnSend);
    mBtnSend.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view)
        {
            mTest= (EditText) findViewById(R.id.txtText);
            mTest.setText("hello");

        }
    });

前面的代码在里面:

@Override
public void onCreate(Bundle savedInstanceState) {

MainActivity.java之后setContentView(R.layout.main);

button 和 EditText 的变量都在这里声明:

public class MainActivity extends Activity implements IToolbarsContainer, OnTouchListener, IDownloadEventsListener {
    private Button mBotonEnviar;
    private EditText mCampoPrueba;

我有:

import android.view.View.OnClickListener;

这是完整的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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"
tools:context="org.cade.codigos.ui.activities.RequestData">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Titulo"
    android:id="@+id/txtTitiulo"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Nombre"
    android:id="@+id/lblNombre"
    android:layout_marginTop="40dp"
    android:layout_below="@+id/txtTitiulo"
    android:layout_alignParentRight="true" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Nombre"
    android:ems="10"
    android:id="@+id/txtNombre"
    android:layout_below="@+id/lblNombre"
    android:layout_alignParentLeft="true"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Apellido"
    android:id="@+id/lblApellido"
    android:layout_below="@+id/txtText"
    android:layout_alignParentLeft="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Apellido"
    android:ems="10"
    android:id="@+id/txtApellido"
    android:layout_below="@+id/lblApellido"
    android:layout_alignParentLeft="true" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send"
    android:id="@+id/btnSend"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" />

Long..请求的onCreate方法

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);              

    INSTANCE = this;

    Constants.initializeConstantsFromResources(this);

    Controller.getInstance().setPreferences(PreferenceManager.getDefaultSharedPreferences(this));       

    if (Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_SHOW_FULL_SCREEN, false)) {          
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    if (Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_GENERAL_HIDE_TITLE_BARS, true)) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }



    setProgressBarVisibility(true);

    //setContentView(R.layout.main);
    setContentView(R.layout.activity_request_data);

    mBotonEnviar = (Button) findViewById(R.id.btnEnviar);
    mBotonEnviar.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view)
        {
            mCampoPrueba = (EditText) findViewById(R.id.txtApellido);
            mCampoPrueba.setText("hola");

        }
    });

    mCircularProgress = getResources().getDrawable(R.drawable.spinner);

    EventController.getInstance().addDownloadListener(this);                

    mHideToolbarsRunnable = null;

    mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    buildComponents();                

    mViewFlipper.removeAllViews();   

    updateSwitchTabsMethod();
    updateBookmarksDatabaseSource();

    registerPreferenceChangeListener();

    Intent i = getIntent();
    if (i.getData() != null) {
        // App first launch from another app.
        addTab(false);
        navigateToUrl(i.getDataString());
    } else {
        // Normal start.
        int currentVersionCode = ApplicationUtils.getApplicationVersionCode(this);
        int savedVersionCode = PreferenceManager.getDefaultSharedPreferences(this).getInt(Constants.PREFERENCES_LAST_VERSION_CODE, -1);

        // If currentVersionCode and savedVersionCode are different, the application has been updated.
        if (currentVersionCode != savedVersionCode) {
            // Save current version code.
            Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
            editor.putInt(Constants.PREFERENCES_LAST_VERSION_CODE, currentVersionCode);
            editor.commit();

            // Display changelog dialog.
            Intent changelogIntent = new Intent(this, ChangelogActivity.class);
            startActivity(changelogIntent);
        }




        /*
        if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREFERENCES_BROWSER_RESTORE_LAST_PAGE, false)) {
            if (savedInstanceState != null) {               
                String savedUrl = savedInstanceState.getString(Constants.EXTRA_SAVED_URL);
                if (savedUrl != null) {
                    addTab(false);
                    navigateToUrl(savedUrl);
                    lastPageRestored = true;
                }
            }
        }*/

        boolean lastPageRestored = false;
        // gaf - comienzo con la pagina inicial
        String savedUrl = "file:///android_asset/startpage/index.html";
        //String savedUrl = "http://servidor-cade.com";
        //String savedUrl = "http://192.168.1.18:3636";
        addTab(false);
        navigateToUrl(savedUrl);
        lastPageRestored = false;

        //if (!lastPageRestored) {
        //  addTab(true);
        //}
    }

    initializeWebIconDatabase();

    startToolbarsHideRunnable();



}

【问题讨论】:

  • 如果在 onClick 方法中设置断点,代码根本就没有到达那里?顺便说一句,您附加的代码中没有定义 mBtnSend 和 mTest
  • 请发布您的完整主要布局。
  • 您确定R.id.btnSend 在您调用findViewById 的视图内吗?
  • 我在 MainActivity.java 中调用 findViewById 并且按钮在 activity_request_data.xml 中
  • 你为什么用 R.layout.main 而不是 R.layout 调用 setContentView()。活动请求数据?

标签: android android-studio onclicklistener


【解决方案1】:

btnEnviar 在哪里? 取 findViewById(R.id.txtApellido);听者之外。 也许试试这个:

mCampoPrueba = (EditText) findViewById(R.id.txtApellido);
mBotonEnviar = (Button) findViewById(R.id.btnEnviar);
    mBotonEnviar.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view)
        {
                            mCampoPrueba.setText("hola");

        }
    });

【讨论】:

  • 它的 btnSend ,我把名字改成了英文,这样你就可以在 xml 中理解了,不用担心名字的问题
  • 您是否将 findViewById 从 onClick 中取出?
【解决方案2】:

我认为你必须在 onCreate 方法中初始化 mTest,因为当你创建一个监听器时,你创建了一个新的匿名对象。如果你想在 MainActivity 中重新调用对象,你必须调用:

mTest= (EditText) MainActivity.this.findViewById(R.id.txtText)

【讨论】:

    【解决方案3】:

    布局文件没有带有R.id.btnEnviar 的按钮。你的意思是:R.id.btnSend

    mCampoPrueba = (EditText) findViewById(R.id.txtApellido);
    mBotonEnviar = (Button) findViewById(R.id.btnSend);
        mBotonEnviar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view)
            {
               mCampoPrueba.setText("hola");
            }
        });
    

    【讨论】:

      【解决方案4】:

      问题是由于我缺乏知识,我在错误的活动中编写代码,非常感谢大家!

      【讨论】:

        猜你喜欢
        • 2013-04-16
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2021-06-09
        • 2018-07-21
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        相关资源
        最近更新 更多