如何仅在按下来自其他视图的某些按钮时才在视图中显示某个ImageView?

时间:2013-05-30 16:50:46

标签: android imageview cordova imagebutton

我有两种不同的布局,title_bar和bottom_bar。 bottom_bar有3个不同的ImageButtons:usersButton,activtyButton和scanButton,而title_bar有一个TextView和几个ImageViews,我感兴趣的ImageView是refreshButton。这是bottom_bar的xml:      

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottomToolBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000000" 
android:tileMode="repeat"
android:gravity="bottom"  >

<ImageButton
    android:id="@+id/usersButton"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="left" 
    android:layout_weight="2" 
    android:background="#000000"        
    android:src="@drawable/users" />

  <ImageButton
      android:id="@+id/activtyButton"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_gravity="left" 
    android:layout_weight="2" 
    android:background="#000000"        
      android:src="@drawable/activity" />

  <ImageButton
      android:id="@+id/scanButton"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_gravity="left" 
        android:layout_weight="2" 
        android:background="#000000"        
      android:src="@drawable/scan" />

</LinearLayout>

并且title_bar xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titleBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff0000" 
android:tileMode="repeat"
android:gravity="top"  >
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="43dip"
    android:src="@drawable/top_bar"
    />

<ImageView
    android:id="@+id/refreshButton"
    android:layout_width="55dip"
    android:layout_height="38dip"
    android:layout_gravity="right"
    android:layout_marginTop="2dip"
    android:src="@drawable/bar_refresh"/>

</FrameLayout>

我想要做的是只在pressButton和activtyButton被按下时才显示refreshButton,而不是scanButton,但是现在它会持续存在于所有3个按钮中。我似乎遇到困难的是看到bottom_bar和title_bar之间的通信关于哪些视图显示,并且使用cordova作为底部和标题之间的中间人,这更加复杂。底栏的java代码是:

public final class BottomBar implements OnClickListener {

private ImageButton usersButton_;
private ImageButton activityButton_;
private ImageButton scanButton_;
private Activity activity_;

public BottomBar(LinearLayout layout, Activity activity) {
    activity_ = activity;
    LayoutInflater inflater;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout bottomBar = (LinearLayout) inflater.inflate(
            R.layout.bottom_bar, null);

    layout.addView(bottomBar);

    usersButton_ = (ImageButton) layout.findViewById(R.id.usersButton);
    activityButton_ = (ImageButton) layout.findViewById(R.id.activtyButton);
    scanButton_ = (ImageButton) layout.findViewById(R.id.scanButton);

    usersButton_.setOnClickListener(this);
    activityButton_.setOnClickListener(this);
    scanButton_.setOnClickListener(this);

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    FrameLayout titleBar = (FrameLayout) inflater.inflate(
            R.layout.title_bar, null);

    layout.addView(titleBar, 0);

    ImageView backButton = (ImageView) layout.findViewById(R.id.backButton);
    backButton.setVisibility(View.GONE);

}

public void onClick(View view) {
    Log.d("getClass", activity_.getClass().getName());

    if (activity_.getClass().getName().contains("CameraActivity")
            || activity_.getClass().getName()
                    .contains("RedemptionActivity"))
        activity_.finish();

    String app_name = ((App) activity_.getApplication())
            .getAppName();
    Log.d("app name", app_name);

    if (view == usersButton_) {
        Log.d("onClick", "usersButton");
        Intent intent = new Intent("com.sampleapp.LOAD_URL");
        intent.putExtra(
                "url",
                "file:///android_asset/www/index.html#activeTab=Tab%253ATerm%253A1&_=GenericScreen&limit=30&offset=0&page=TabbedPage%253ATerm%253A1&app_name="
                        + app_name);
        activity_.sendBroadcast(intent);
    } else if (view == activityButton_) {
        Log.d("onClick", "activityButton");
        Intent intent = new Intent("com.samplepp.LOAD_URL.LOAD_URL");
        intent.putExtra(
                "url",
                "file:///android_asset/www/index.html#activeTab=Tab%253ATerm%253A2&_=GenericScreen&limit=30&offset=0&page=TabbedPage%253ATerm%253A2&app_name="
                        + app_name);
        activity_.sendBroadcast(intent);
    } else if (view == scanButton_) {
        Log.d("onClick", "scanButton");
        Intent intent = new Intent(activity_, CameraActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity_.startActivity(intent);
    }

}

}

title_bar的java代码:

public class Title extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
        final CallbackContext callbackContext) throws JSONException {
    Log.d("Title", action);
    if (action.equals("change")) {
        Intent intent = new Intent("com.sampleapp.CHANGE_TITLE");
        intent.putExtra("title", args.getString(0));
        this.cordova.getActivity().sendBroadcast(intent);
        return true;
    }
    return false;
}
}

最后是cordova的java代码:

public class Cordova extends DroidGap {
TextView title;
ImageView backButton;
ImageView refreshButton;

private void changeTitle(String url) {
    if (url.contains("Term%253A1"))
        title.setText("App Users");
    if (url.contains("Term%253A2"))
        title.setText("User Activity");

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.setIntegerProperty("loadUrlTimeoutValue", 60000);

    registerReceiver(mHandleUrlReceiver, new IntentFilter(
            "com.sampleapp.LOAD_URL"));

    registerReceiver(mHandleTitleReceiver, new IntentFilter(
            "com.sampleapp.CHANGE_TITLE"));

    this.init();

    this.appView.clearCache(true);
    this.appView.clearHistory();

    new BottomBar(this.root, this);

    backButton = (ImageView) root.findViewById(R.id.backButton);
    backButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            appView.goBack();
            backButton.setVisibility(View.GONE);
            changeTitle(appView.getUrl());
        }
    });

    /*refreshButton = (ImageView) root.findViewById(R.id.refreshButton);
    refreshButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
        }
    });*/

    title = (TextView) this.root.findViewById(R.id.title);

    spinnerStart("Loading", "Please wait...");

    if (this.getIntent().hasExtra("json")) {
        String json = this.getIntent().getExtras().getString("json");
        Log.d("CORDOVA JSON", json);

        try {
            JSONObject jObject = new JSONObject(json);
            Log.d("CORDOVA JSON", jObject.getString("app_name"));
            ((RobotFruitMerchant) this.getApplication())
                    .setSessionId(jObject.getString("session_id"));

            String url = "file:///android_asset/www/index.html#activeTab=Tab%253ATerm%253A1&_=GenericScreen&page=TabbedPage%253ATerm%253A1&limit=30&offset=0&sessionId="
                    + jObject.getString("session_id")
                    + "&userId="
                    + jObject.getJSONObject("user").getString("id")
                    + "&app_name=" + jObject.getString("app_name");
            super.loadUrl(url);
            title.setText("App Users");
        } catch (JSONException e) {
            Log.e("JSONException", e.getMessage());
            throw new RuntimeException(e);
        }

    } else {
        String url = this.getIntent().getExtras().getString("url");
        super.loadUrl(url);
    }

}

private final BroadcastReceiver mHandleUrlReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String url = intent.getExtras().getString("url");
        Log.d("CordovaReceiver", url);
        appView.loadUrl(url);
        changeTitle(url);

    }
};

private final BroadcastReceiver mHandleTitleReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String title_text = intent.getExtras().getString("title");
        title.setText(title_text);
        backButton.setVisibility(View.VISIBLE);

    }
};

}

抱歉,如果它有点冗长,我相对较新,并希望尽可能具体。任何提示/建议/建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的,我想我找到了一个解决方法。我所做的是将refreshButton声明为公共静态int,以便其他类可以访问它,然后在按下scanButton时调用的CameraActivity类中调用BottomBar.refreshButton.setVisibility(View.GONE)。它似乎暂时有用,但我不确定它一般是多么安全或实用。如果有人有更好的方法,或者我不应该这样做的原因,请告诉我。谢谢!