给按扭添加监听事件,可按钮按了以后还是没反应

YXTS122 2016-09-06 09:34:18
MainActivity.java
package com.example.ch02_marry;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Intent intent=new Intent();
intent.setClass(MainActivity.this,ResultActivity.class);
MainActivity.this.startActivityForResult(intent,0);

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


ResultActivity.java
package com.example.ch02_marry;

import android.app.Activity;
import android.os.Bundle;


public class ResultActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_main);

}

}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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.ch02_marry.MainActivity"
android:stretchColumns="2">

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_span="3"
android:text="@string/app_name"
android:textSize="35sp"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/laogong"
android:textSize="25sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/mingzi"
android:textSize="35sp" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/dianhua"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/chusheng"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/zhuzhi"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/laopo"
android:textSize="25sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/ming"
android:textSize="35sp" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/dianhua"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/chusheng"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>

<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/zhuzhi"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>

<TableRow>
<Button
android:id="@+id/bt1"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="center"
android:text="@string/marry"
android:textSize="20sp" />
</TableRow>

</TableLayout>


result_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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.ch02_marry.MainActivity"
android:orientation="vertical">

<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/wenben"
android:textSize="45sp"/>

</LinearLayout>


strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">结婚证</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="marry">结婚</string>
<string name="nam">名字</string>
<string name="laogong">丈夫</string>
<string name="laopo">妻子</string>
<string name="mingzi">悟空</string>
<string name="dianhua">电话</string>
<string name="chusheng">出生日期</string>
<string name="zhuzhi">住址</string>
<string name="ming">八戒</string>
<string name="wenben">祝白头到老</string>
</resources>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ch02_marry"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ch02_marry.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.ch02_marry.ResultActivity"
android:label="@string/app_name"
></activity>
</application>

</manifest>




...全文
382 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2016-09-07
  • 打赏
  • 举报
回复
MainActivity.java
package com.example.ch02_marry;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(this);
TextView textView = (TextView) findViewById(R.id.tv1);
textView.setText(Html.fromHtml(descString(), getImageGetterInstance(),null));
TextView textView2 = (TextView) findViewById(R.id.tv2);
textView2.setText(Html.fromHtml(descStrings(), getImageGetterInstance(),null));
}



@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Intent intent=new Intent();
intent.setClass(MainActivity.this,ResultActivity.class);
MainActivity.this.startActivityForResult(intent,0);
break;
default:break;

}
}

private String descString() {
return "<img src='" + R.drawable.monkey + "'/>";
}

private String descStrings() {
return "<img src='" + R.drawable.pig + "'/>";
}

public ImageGetter getImageGetterInstance() {
ImageGetter imgGetter = new Html.ImageGetter() {

@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable d = getResources().getDrawable(id);
int wdp = dip2px(getApplicationContext(), 100);
int hdp = dip2px(getApplicationContext(), 58);
Log.e("wdp", wdp + "");
Log.e("hdp", hdp + "");
d.setBounds(0, 0, wdp, hdp);
return d;
}
};
return imgGetter;
}

public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
else if (id==R.id.zhuxiao_item) {
Toast toast1=Toast.makeText(getApplicationContext(),"你已离婚了!",Toast.LENGTH_SHORT);
toast1.show();
}
else if (id==R.id.chongban_item) {
Toast toast1=Toast.makeText(getApplicationContext(),"你已复婚了!",Toast.LENGTH_SHORT);
toast1.show();
}
return super.onOptionsItemSelected(item);
}
}



main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ch02_marry.MainActivity" >

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>

<item
android:id="@+id/zhuxiao_item"
android:title="@string/zhuxiao"/>

<item
android:id="@+id/chongban_item"
android:title="@string/chongban"/>

</menu>


效果如图

乐之者v 2016-09-06
  • 打赏
  • 举报
回复
引用 4 楼 qq_33225439 的回复:
唐三藏老婆是西经 先获取控件,findViewById
虽然很冷,但我还是笑了。哈哈
b2bo2o 2016-09-06
  • 打赏
  • 举报
回复
先在MainActivity 通过findviewbyid找到控件,然后设置控件的setonclicklistener
秃头地中海 2016-09-06
  • 打赏
  • 举报
回复
唐三藏老婆是西经 先获取控件,findViewById
ink_s 2016-09-06
  • 打赏
  • 举报
回复
Button button = (Button)findViewById(R.id.bt1); Button button = (Button)findViewById(R.id.bt1); Button button = (Button)findViewById(R.id.bt1); Button button = (Button)findViewById(R.id.bt1);
ink_s 2016-09-06
  • 打赏
  • 举报
回复
Button button = (Button)relativeLayout.findViewById(R.id.bt1); Button button = (Button)relativeLayout.findViewById(R.id.bt1); Button button = (Button)relativeLayout.findViewById(R.id.bt1); Button button = (Button)relativeLayout.findViewById(R.id.bt1);
ink_s 2016-09-06
  • 打赏
  • 举报
回复
悟空表示 我不娶八戒!!! 主要是 兄弟 ???你的按钮在哪 ?在哪?在哪? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt1: Intent intent=new Intent(); intent.setClass(MainActivity.this,ResultActivity.class); MainActivity.this.startActivityForResult(intent,0); } } ????? findbyid 呢????

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧