关于非主动性的OnClickListener&更改第一项活动

时间:2015-09-18 09:22:55

标签: android login onclicklistener

我有一个问题。我想创建一个具有loggin活动和主要活动的应用程序。 (稍后给OnClickListiner)

拳头:

到目前为止我做了什么:

  • 我创建了一个登录信息。 java和login.xml login.java:

    public class Login extends Activity implements OnClickListener {
        Button btnStartAnotherActivity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
    
            boolean hasLogedIn = true;
            if (hasLogedIn) {
                Intent i = new Intent(Login.this, MainActivity.class);
                startActivity(i);
                finish();
            } else {
            }
        }
    
    
        public void onClick(View view) {
            //calling an activity using <intent-filter> action name
            Intent inent = new Intent("android.name.MainActivity ");
    
            startActivity(inent);
        }
    }
    
  • 我创建了一个MainActivity.java和activity_main.xml

  • 此外,还有一些其他java和xml文件,用于为我的MainActivity制作物料设计的Tab视图。
  • 我有3个标签,了解到目前为止的样子

[![tab1 with buttons] [1]] [1]

- 现在我被添加到第一个标签3按钮。

一个按钮的示例:(其他按钮只是另一个ID)

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kommt"
        android:clickable="false"
        android:textColor="#190707"
        android:id="@+id/kommt"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/space" />

- 所以应用程序现在运行没有问题。 (所有标签和内容显示我想要它)

  • 当我构建项目时,mainactivity首先打开(参见图片链接)
  • 如何设置:第一次启动应用程序时,启动一次登录界面。登录成功后,主活动将始终打开。这是我的第一个问题。我应该向Login.java添加什么?以及如何设置login.xml在mainactivity之前启动?

第二

正如我告诉你的,我添加了一些按钮。为了测试按钮,我在点击按钮时试图实现Toast通知代码。但每次我用toast通知代码构建项目时,应用程序都会重新开始。这里的吐司通知代码使用:

public class MainActivity extends ActionBarActivity implements OnClickListener { 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b

Button kommtbutton;

kommtbutton= (Button) findViewById(R.id.kommt);
        kommtbutton.setOnClickListener(this);}



@Override
    public void onClick(View v) {

        setContentView(R.layout.tab1); // not sure if this is right ive did it cause the buttons are in the tab1 layout and not main_activity

        switch(v.getId())
        {
            case R.id.kommt:
            {
                Toast toast1 = Toast.makeText(getApplicationContext(),
                        "Eingestempelt",
                        Toast.LENGTH_SHORT);

                toast1.show();
                break;
            } ....}

这里我已经在开关盒中实现了所有3个按钮。

它看起来是正确的,但onclicklistinier似乎在它开始之前杀死了我的应用程序。也许有人可以帮助我。

我有以下文件:

Login.java,MainActivity.java,Tab1.java,Tab2.java,Tab3.java,SlidingTabsLayout.java,SlidingTabStrip.java和ViewPagerAdaper。

我有这个布局。

acitivy_main.xml,login.xml,tab1,tab2,tab3.xml,toolbar.xml。

我不允许发送图片,因为我在这里很新。

我在哪里实现Toast通知的代码?

那就是我的activity_main:xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />

<android_package.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/ColorPrimary"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"

    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1"/>

这是我的tab1.xml

 android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Tabs"
    android:background="#FDFDFE"
    >



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/buchungen"
        android:textColor="#190707"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Geht"
        android:clickable="false"
        android:textColor="#190707"
        android:id="@+id/geht"
        android:layout_alignParentBottom="true"
        android:layout_alignEnd="@+id/textClock"
        android:layout_marginBottom="36dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name:"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:textColor="#190707"
        android:id="@+id/name"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/buchungen" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#190707"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Status:"
        android:id="@+id/status"
        android:layout_below="@+id/name"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#190707"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Letzte Buchung:"
        android:paddingBottom="4dp"
        android:paddingTop="7dp"
        android:id="@+id/letzteBuchung"
        android:layout_below="@+id/status"
        android:layout_alignParentStart="true" />


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_alignBottom="@+id/buchungen"
        android:weightSum="1"
        android:id="@+id/linearLayout">

        <ImageView
            android:layout_width="314dp"
            android:layout_height="310dp"
            android:id="@+id/profilbild"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/time" />

    </LinearLayout>

    <Space
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_above="@+id/geht"
        android:id="@+id/space" />

    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#190707"
        android:id="@+id/textClock"
        android:layout_alignTop="@+id/name"
        android:layout_alignParentEnd="true" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kommt"
        android:clickable="false"
        android:textColor="#190707"
        android:id="@+id/kommt"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/space" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Pause An/Aus"
        android:textColor="#190707"
        android:id="@+id/textView"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/pause" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Einstempeln"
        android:textColor="#190707"
        android:id="@+id/textView2"
        android:layout_above="@+id/kommt"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Ausstempeln"
        android:textColor="#190707"
        android:id="@+id/textView3"
        android:layout_alignBottom="@+id/geht"
        android:layout_alignStart="@+id/geht"
        android:layout_alignTop="@+id/textView2" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause"
        android:textColor="#190707"
        android:id="@+id/pause"
        android:layout_alignBottom="@+id/kommt"
        android:layout_centerHorizontal="true"
        android:checked="false" />

</RelativeLayout>

按钮位于tab1.xml中。我在哪里可以访问它们进行交互(按下按钮时显示吐司)?在MainActivity.java或Tab1.java或其他地方?

当我尝试添加吐司通知时,我的应用只会自行杀死......

1 个答案:

答案 0 :(得分:1)

您必须将所有findViewById和setContentView放入onCreate方法中,否则他们将无法胜任。

第一点:

public class MainActivity extends ActionBarActivity
{
    // Layout elements
    private Button kommtbutton = null;

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

        // Attach layout
        setContentView(R.layout.activity_main); // but my main_activity doesnt have buttons ... tab1.xml have b

        // Retrieve layout elements
        kommtbutton= (Button) findViewById(R.id.kommt);

        // Attach listeners
        kommtbutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view)
            {
                // Do not use getApplicationContext(), this is an activity
                Toast.makeText(MainActivity.this, "Eingestempelt", Toast.LENGTH_SHORT).show();
            }
        });
    }

    [...]
}

第二点:

public class LoginActivity extends Activity
{
    // Layout elements
    private EditText edit_login = null;
    private EditText edit_password = null;
    private Button btn_login = null;

    // Class variables
    private SharedPreferences prefs = null;

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

        // Check if the user is already logged in
        prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
        if (prefs.getBoolean("isLoggedIn", false))
        {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        // Attach layout
        setContentView(R.layout.login);

        // Retrieve layout elements
        edit_login = (EditText) findViewById(R.id.edit_login);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_login = (Button) findViewById(R.id.btn_login);

        // Attach listeners
        btn_login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view)
            {
                // Retrieve information
                String login = edit_login.getText().toString();
                String password = edit_password.getText().toString();

                // Do job
                boolean canConnect = true; // TODO
                if (canConnect)
                {
                    // Update prefs
                    prefs.edit().putBoolean("isLoggedIn", true).commit();

                    // Move to activity
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else
                {
                    // Update prefs
                    prefs.edit().putBoolean("isLoggedIn", false).commit();

                    // Display error message
                    Toast.makeText(LoginActivity.this, "Wrong crendentials", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}