0

主屏幕上的图像是调用活动的按钮操作栏上的按钮是否可以调用与主屏幕上的按钮相同的活动..我应该制作什么样的按钮才能调用活动....我在菜单布局中声明了操作栏,如果它有助于确定我如何制作操作栏..问题是要使用的按钮,因此它可以调用活动..

这是我的菜单 xml 文件

<item 
    android:id="@+id/open" 
    android:showAsAction="ifRoom|withText" 
    android:title="open" 
    android:icon="@drawable/ic_action_open" 
    android:menuCategory="secondary"></item>
<item 
    android:id="@+id/save" 
    android:showAsAction="ifRoom|withText" 
    android:title="Save" 
    android:icon="@drawable/ic_action_save"></item>
<item 
    android:id="@+id/close" 
    android:showAsAction="ifRoom|withText" 
    android:title="Close" 
    android:icon="@drawable/ic_action_close"></item>
<item 
    android:id="@+id/history" 
    android:showAsAction="ifRoom|withText" 
    android:title="History" 
    android:icon="@drawable/ic_action_history"></item>
<item 
    android:id="@+id/settings" 
    android:showAsAction="ifRoom|withText" 
    android:title="Settings" 
    android:icon="@drawable/ic_action_settings"></item>


</menu>

我的 .java 文件

package name.brucephillips.actionbarexample;

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

public class ActionBarExampleActivity extends Activity {
/** Called when the activity is first created. */
 @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    }
    public void sample (View v)
      {
    Intent intent = new Intent(getApplicationContext(), Sample.class); 
    startActivity(intent);
    }
    public void sample1(View v)
    {
    Intent intent = new Intent(getApplicationContext(), Sample1.class); 
    startActivity(intent);
    }
     public void sample2(View v)
    {
    Intent intent = new Intent(getApplicationContext(), Sample2.class); 
    startActivity(intent);
    }
    public void sample3(View v) {
    Intent intent = new Intent(getApplicationContext(), Sample3.class);
    startActivity(intent);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
            case R.id.sample:
            startActivity(new Intent(getApplicationContext(), Sample.class));
            break;
            case R.id.sample1:
            startActivity(new Intent(getApplicationContext(), Sample1.class));
            break;
            case R.id.sample2:
            startActivity(new Intent(getApplicationContext(), Sample2.class));
            break;
            case R.id.sample3:
            startActivity(new Intent(getApplicationContext(), Sample3.class));
            break;
    }
        return super.onOptionsItemSelected(item);
    }
}

这是我的主要 xml

android:orientation="vertical">

 <ImageView
 android:id="@+id/sample"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="sample"
 android:src="@drawable/ic_action_close" />

<ImageView
android:id="@+id/sample1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample1"`enter code here`
android:src="@drawable/ic_action_history" />

<ImageView
android:id="@+id/sample2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample2"
android:src="@drawable/ic_action_open" />

<ImageButton
android:id="@+id/sample3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample3"
android:src="@drawable/ic_action_save" />



</LinearLayout>
4

2 回答 2

3

解决此问题的另一种方法

@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) {
            startActivity(new Intent(getApplicationContext(),Preferencia.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

您可以在不实现 onClickListener 的情况下调用新活动 onOptionItem...

于 2014-11-27T23:43:55.990 回答
1

这是我最好的例子,玩得开心!

public class MainActivity extends Activity implements OnClickListener {
ImageButton imageButton;
ImageView imageView1;
ImageView imageView2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageButton = (ImageButton) findViewById(R.id.imageButton1);
    imageView1 = (ImageView) findViewById(R.id.imageView1);
    imageView2 = (ImageView) findViewById(R.id.imageView2);

    imageButton.setOnClickListener(this);
    imageView1.setOnClickListener(this);
    imageView2.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.item1:
        onClick(imageButton);
        break;
    case R.id.item2:
        onClick(imageView1);
        break;
    case R.id.item3:
        onClick(imageView2);
        break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.imageButton1:
         startActivity(new Intent(getApplicationContext(), open.class));
        break;
    case R.id.imageView1:

         startActivity(new Intent(getApplicationContext(), close.class));
        break;
    case R.id.imageView2:

         startActivity(new Intent(getApplicationContext(), save.class));

        break;

    }
}

}
于 2013-03-26T04:04:40.370 回答