问题描述
- 一个简单的单击事件监听代码哪里错了?
- MainActivity.java
package com.example.a007;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends ActionBarActivity { private EditText myet1; private Button mybtn1; private TextView mytv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.myet1=(EditText)super.findViewById(R.id.et1); this.mybtn1=(Button)super.findViewById(R.id.btn1); this.mytv1=(TextView)super.findViewById(R.id.tv1); mybtn1.setOnClickListener(new ShowListener()); //注册监听 } private class ShowListener implements OnClickListener{ //定义监听处理程序 public void onClick(View v){ String info=myet1.getText().toString(); mytv1.setText(""Your telephone number is:""+info); } }}
activity_main.xml
<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:paddingBottom=""@dimen/activity_vertical_margin"" android:paddingLeft=""@dimen/activity_horizontal_margin"" android:paddingRight=""@dimen/activity_horizontal_margin"" android:paddingTop=""@dimen/activity_vertical_margin"" tools:context=""com.example.a007.MainActivity"" > <EditText android:id=""@+id/et1"" android:layout_width=""wrap_content"" android:layout_height=""wrap_content"" android:textSize=""20px"" android:phoneNumber=""true"" android:selectAllOnFocus=""true"" android:text=""Please input your telephone number:"" /> <Button android:id=""@+id/btn1"" android:layout_width=""wrap_content"" android:layout_height=""wrap_content"" android:layout_below=""@+id/et1"" android:layout_marginTop=""20dp"" android:textSize=""20px"" android:text=""Show your telephone number"" /> <View android:layout_height=""1dp"" android:background=""#909090"" /> <TextView android:id=""@+id/tv1"" android:layout_width=""wrap_content"" android:layout_height=""wrap_content"" android:layout_below=""@+id/btn1"" android:layout_marginTop=""20dp"" android:textSize=""20px"" /></RelativeLayout>
程序运行不起来,stopped unexpectedly..
解决方案
class ButtonClick implements View.OnClickListener
{
@Override
public void onClick(View v)
{
}
}
最好能将出错的 LOG 帖出来, 这样好分析问题的原因在哪里?
解决方案二:
mybtn1.setOnClickListener(new ShowListener()); //注册监听
}
private class ShowListener implements OnClickListener{ //定义监听处理程序
public void onClick(View v){
String info=myet1.getText().toString();
mytv1.setText(""Your telephone number is:""+info);
}
}
}
你设置的是click监听器,为什么传递对象是showlistener呢?
时间: 2024-10-30 20:47:36