如何在Android中关联按钮?
例如在主页中有2个按钮:' Item1& ITEM2'
我需要输入代码,这样我就可以点击'项目'按钮和Item1数据将显示...
这是我在Activity.java上的当前代码:
package com.example.myshops;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Display;
import android.view.WindowManager;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Home_fragment hf=new Home_fragment();
fragmentTransaction.replace(android.R.id.content, hf);
fragmentTransaction.commit();
setContentView(R.layout.activity_main);
Button item1Click = (Button)findViewById(R.id.Item1Button);
itemClick1.setOnClickListener(this);
Button item2Click = (Button)findViewById(R.id.Item2Button);
itemClick2.setOnClickListener(this);
Button item3Click = (Button)findViewById(R.id.Item3Button);
itemClick3.setOnClickListener(this);
Calendar c = Calendar.getInstance();
System.out.println("Current time => "+c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
// formattedDate have current date/time
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
// Now we display formattedDate value in TextView
TextView txtView = new TextView(this);
txtView.setText("Current Date and Time : "+formattedDate);
txtView.setGravity(Gravity.CENTER);
txtView.setTextSize(20);
setContentView(txtView);
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
public void item1Click(View view) {
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Item1_fragment hf=new Item1_fragment();
fragmentTransaction.replace(android.R.id.content, hf);
fragmentTransaction.commit();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.Item1Button:
break;
case R.id.Item2Button:
break;
case R.id.Item3Button:
break;
}
}
}
我的home.xml(例如):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myshops.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/Item2Button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/Item2Button"
android:onClick="item2Click"
android:text="@string/Item2" />
<Button
android:id="@+id/Item3Button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/Item2Button"
android:onClick="item3Click"
android:text="@string/Item3" />
<Button
android:id="@+id/Item1Button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="item1Click"
android:text="@string/Item1" />
<Button
android:id="@+id/PrevButton"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/Previous" />
<Button
android:id="@+id/HomeButton"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/Home" />
<Button
android:id="@+id/NextButton"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/Next" />
</RelativeLayout>
我的记录中出现错误:
04-11 15:56:54.915: D/dalvikvm(547): Not late-enabling CheckJNI (already on)
04-11 15:56:56.145: D/AndroidRuntime(547): Shutting down VM
04-11 15:56:56.155: W/dalvikvm(547): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-11 15:56:56.165: E/AndroidRuntime(547): FATAL EXCEPTION: main
04-11 15:56:56.165: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s0237600_diary/com.example.myshops.MainActivity}: java.lang.NullPointerException
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:137)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-11 15:56:56.165: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 15:56:56.165: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 15:56:56.165: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-11 15:56:56.165: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-11 15:56:56.165: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
04-11 15:56:56.165: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
04-11 15:56:56.165: E/AndroidRuntime(547): at com.example.myshops.MainActivity.onCreate(MainActivity.java:44)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.Activity.performCreate(Activity.java:4465)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-11 15:56:56.165: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-11 15:56:56.165: E/AndroidRuntime(547): ... 11 more
04-11 15:56:58.955: I/Process(547): Sending signal. PID: 547 SIG: 9
04-11 15:57:02.035: D/AndroidRuntime(565): Shutting down VM
04-11 15:57:02.035: W/dalvikvm(565): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-11 15:57:02.055: E/AndroidRuntime(565): FATAL EXCEPTION: main
04-11 15:57:02.055: E/AndroidRuntime(565): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myshops/com.example.myshops.MainActivity}: java.lang.NullPointerException
04-11 15:57:02.055: E/AndroidRuntime(565): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-11 15:57:02.055: E/AndroidRuntime(565): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-11 15:57:02.055: E/AndroidRuntime(565): at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-11 15:57:02.055: E/AndroidRuntime(565): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-11 15:57:02.055: E/AndroidRuntime(565): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 15:57:02.055: E/AndroidRuntime(565): at android.os.Looper.loop(Looper.java:137)
04-11 15:57:02.055: E/AndroidRuntime(565): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-11 15:57:02.055: E/AndroidRuntime(565): at java.lang.reflect.Method.invokeNative(Native Method)
答案 0 :(得分:0)
您应该在代码中或在xml中定义单击侦听器,而不是两者。删除其中一个,另一个应该工作。
答案 1 :(得分:0)
使用
Onclicklistener喜欢
Button item1Click = (Button)findViewById(R.id.Item1Button);
item1Click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
和其他按钮相同
答案 2 :(得分:0)
这样做
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//check here that to which view does v match for example
if(v==item1Click)
{
//then do something
}
}
或者你可以删除那个实现的东西,并为每个按钮创建单独的监听器,如raj
所述答案 3 :(得分:0)
使用单击侦听器访问您的所有Button
点击侦听器,并使用按钮的ID并实现每个点击事件,如下所示:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.Item1Button:
//write your logic
break;
case R.id.Item2Button:
//write your logic
break;
case R.id.Item3Button:
//write your logic
break;
}
}
答案 4 :(得分:0)
我告诉一种解决问题的方法,还有其他方法来解决问题
<Button
android:id="@+id/Item3Button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/Item2Button"
android:onClick="item3Click"
android:text="@string/Item3" />
此处您已为所有按钮编写了android:onClick="item3Click"
,
所以在jave代码中你可以像
那样写 public void item3Click(View view){
//what ever you want to do in the button 3 click
}
public void item2Click(View view){
//what ever you want to do in the button 2 click
}
public void item1Click(View view){
//what ever you want to do in the button 1 click
}
对所有按钮重复相同的操作,