2014-04-11 65 views
-1

如何鏈接Android中的按鈕?如何在Android中將按鈕鏈接到其他xml?

例如在主頁上有2個按鈕:「項目1 & ITEM2」

,我需要把代碼這樣我就可以點擊「項目」按鈕,項目1的數據將顯示.. 。

這是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> 

錯誤在我logcats:

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) 
+1

你能再次解釋一下你真的特林做 – Sree

+0

這很簡單。實現Item1Button點擊事件並獲取onclick Item1Button中的數據並將其設置爲Text2到Item2Button –

+0

@Sreekanthss主頁上有3個按鈕,如果單擊按鈕,我想顯示item1中的內容...但所有按鈕不工作,甚至最糟糕的應用程序總是無法運行(當我雙擊應用程序,它停止) – user3514625

回答

0

您應該在代碼或XML不能同時定義點擊收聽。刪除其中一個,另一個應該工作。

+0

我只在java中定義了click監聽器而不是xml .. – user3514625

+0

<按鈕 機器人:ID = 「@ + ID/Item3Button」 機器人:layout_width = 「200dp」 機器人:layout_height = 「WRAP_CONTENT」 機器人:layout_alignParentLeft = 「真」 機器人:layout_alignParentRight = 「真」 機器人: layout_below = 「@ + ID/Item2Button」 安卓的onClick = 「item3Click」 機器人:文本= 「@字符串/項目3」/> 從所有這些機器人刪除此行:的onClick = 「item3Click」 –

+0

只是從每一個XML點擊線? – user3514625

0

使用

Onclicklistener像

Button item1Click = (Button)findViewById(R.id.Item1Button); 

item1Click.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }; 

,並同其他按鈕

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

所述次
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;  
    } 
} 
+0

@GrlsHu現在的問題是我不能運行我的應用程序... 這是我從調試中發現的錯誤:(!mInstrumentation.onException(活動,E)) }趕上(例外五){ 如果{ throw new RuntimeException( 「Unable to start activity」+ component +「:」+ e.toString(),e); – user3514625

+0

請在您的問題@ user3514625 – GrIsHu

+0

@ user3514625上發佈您的完整logcat爲什麼您要再次在'setContentView(txtView);'行中用'TextView'設置您的佈局。如果您再次設置佈局,那麼您將無法再訪問按鈕,因爲您已經更改了佈局。 – GrIsHu

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",所有按鈕,

所以在java的代碼,你可以這樣寫

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 
    } 

重複的所有按鈕一樣,

相關問題