【问题标题】:Buttons in a custom view when the custom view is in a tab当自定义视图位于选项卡中时,自定义视图中的按钮
【发布时间】:2012-06-20 00:26:07
【问题描述】:

问题

是否可以从与其他三个不同的 Activity 或 Java 文件运行一个选项卡,从而允许我将第二个 Activity 连接到正在运行第一个的服务器。

关于我的应用的一些信息

我拥有的是一个多选项卡式应用程序,具有几种不同的布局。其中之一是交互式图形布局。在这个选项卡上,大部分需要是我已经设置为可以工作的自定义视图,但我需要在底部有一排小部件,如按钮、滑块等。我现在正处于运行图形布局的位置将其设置为清单中的主要活动(android:name=".Tab3Activity"),当我单击按钮时,应用程序将运行并打印出文本但是如果我从选项卡运行它,因为我需要它来运行应用程序将在我单击时崩溃按钮上。这是我目前拥有的代码

标签设置

    th.setup();
    TabSpec specs = th.newTabSpec("tag0");
    specs.setContent(R.id.connecttionTab);
    specs.setIndicator("Connection Tab");
    th.addTab(specs);
    specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("Zone Manager");
    th.addTab(specs);
    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("",res.getDrawable(R.drawable.ic_tab_vaccontrol));
    th.addTab(specs);
    //Intent intent = new Intent().setClass(this, Tab3Activity.class); tryed using this but
    //with this tab goes ontop of all the other ones and still doesnt work.
    TabSpec spec = th.newTabSpec("Tab3").setIndicator("Graphical Layout").setContent(R.id.tab3);
    th.addTab(spec);

Tab3Activity

public class Tab3Activity extends Activity 
{
    private Tab3 mTab3;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab3);
        mTab3 = (Tab3) findViewById(R.id.tab3_display);
    }

    public void onAddZone(View view)
    {
        mTab3.addZone();
    }
}

Tab3(视图类)

public class Tab3 extends View implements OnTouchListener
{
    public Tab3(Context context, AttributeSet attrs)
    {
        super (context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true); 
        setOnTouchListener(this);
        requestFocus();
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        //drawings
        invalidate();
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        //onTouch
        return true;
    }   
    public void addZone() 
    {
        //String w = "W~20~0~0~0~0~0";
        Log.d("ZoneSize", "Zone set");
        //MyApplication.preferences.edit().putString( "ZoneSize", w ).commit();
        //MyApplication.preferences.edit().putBoolean("ZoneSizeReady", true).commit();
    }
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/myLayout"
    android:orientation="vertical"
    android:layout_height="match_parent" >

    <com.zone.manager.Tab3
        android:id="@+id/tab3_display"
        android:layout_width="fill_parent"
        android:layout_height="620dp" />

         <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="match_parent" >   

            <Button
                android:id="@+id/addZone"
                android:layout_width="wrap_content"
                android:onClick="onAddZone"
                android:layout_height="wrap_content"
                android:text="Add Zone" />

            <Button
                android:id="@+id/helpZone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Help" />

            <SeekBar
                android:id="@+id/seekBar1"
                android:paddingTop="9dp"
                android:layout_width="179dp"
                android:layout_height="wrap_content" />

        </LinearLayout>
</LinearLayout>

感谢您对我的任何帮助!

【问题讨论】:

  • StackOverflow 用于编程问题。这不是一个问题。此外,您提到了崩溃,但您没有提供与该崩溃相关的堆栈跟踪。
  • 抱歉,这不是一个问题吗?我在问您如何在自定义视图类中有按钮。
  • 在英语和大多数其他欧洲语言中,问题用问号(“?”)表示,例如您上述评论第一句中的问题。您的帖子缺少任何这些,或者任何可以合理地解释为问题的句子,只是缺少支持标点符号。这个“问题”我已经看了好几遍了,即使有你上面的评论,我仍然不知道你在说什么。
  • 我知道你的意思。我已经更新了我的问题,所以现在它显示了我的确切问题。对此感到抱歉。
  • Michael,如果您希望 SO 社区诊断您遇到的崩溃,您真的应该考虑发布崩溃日志。

标签: android xml view tabs


【解决方案1】:

虽然我们希望您发布崩溃日志,但我建议您不要使用旧的选项卡方法(当然,除非您的客户确实需要这些选项卡,否则您必须这样做)。 Android 的 ui 模式自那个标签小部件以来已经发生了巨大的变化。

实现选项卡的现代方式是使用ActionBarFragments API,它们自 Honeycomb 以来就已存在,但可用于早期的 android 版本作为支持库,您可以使用 android sdk 管理器下载。基于片段的方法将为您省去很多麻烦。

至于您的主要问题,尚不清楚您要通过从不同的活动启动某些内容来完成什么,但是如果您需要从一个活动启动一个长时间运行的任务并将结果返回给另一个,您可以想阅读Services (尤其是IntentService 很可能是您要查找的内容)

【讨论】:

  • 这不是我需要做的,但是我已经弄清楚了如何做我自己。不过还是谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多