Buttons in a custom view when the custom view is in a tab

廉价感情. 提交于 2019-12-10 16:31:05

问题


THE QUESTION

Is it possible to run one tab from a different Activity or Java file than the other three, thus allowing me to connect the second Activity to the server that is running off of the first.

Some information on my app

What I have is a multiple tabbed app, with a few different layouts. One of them is an interactive graphical layout. On this tab most of it needs to be a custom view that I have set up to work already but I need a row of widgets on the bottom like buttons, sliders etc. I am now at the point where if I run the graphical layout by setting it as the main activity (android:name=".Tab3Activity") in the manifest the app will run and print out text when I click on the button how ever if I run it from the tab as I need it to run the app will crash when I click on the button. Here is the code I have currently

Tab set up

    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 (view class)

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>

Thanks for any help you may give to me!


回答1:


While we're expecting you to post your crash log, I suggest you don't use the old tab approach (unless, of course, you're forced to by your client who wants exactly those tabs). Android's ui patterns have severely evolved since that tab widget thing.

The contemporary way to implement tabs is to use ActionBar and Fragments API, which have been around since Honeycomb but are available for earlier android versions as a support library you can download with your android sdk manager. Fragment-based approach will save you a lot of headache.

As for your main question, it's not quite clear what you're trying to accomplish with launching something from different activities but if you need a long-running task to be launched from one activity and return the result to another, you may want to read up on Services (particularly, IntentService is most likely what you're looking for)



来源:https://stackoverflow.com/questions/11075992/buttons-in-a-custom-view-when-the-custom-view-is-in-a-tab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!