2015-09-18 36 views
0

我有问题。我想制作一个有登录活动和主要活动的应用程序。 (对OnClickListiner后)OnClickListener on non-Mainactivity&change first activity

拳头:

什么香港专业教育学院迄今所做的:

  • 我创建了一个登录。 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。
  • 我有3个标签这就是它的外观,到目前为止

[!用按钮[TAB1] [1] [1]

-now i'be加入到第一片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之前启动?

二:

正如我告诉你香港专业教育学院增加了一些按钮。为了测试按钮,我尝试在单击按钮时实现烤面包通知的代码。但每次我使用吐司通知代码构建项目时,该应用程序都会再次启动。在这里我使用的吐司通知的代码:

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或其他地方?

当我尝试添加Toast通知我的应用程序只是杀死自己...

+0

Vitja,请不要使用谷歌驱动器以显示图像,没有人有权限查看https://drive.google.com /文件/ d/0B3EdZ8mF6GzzYnFNUlRzbXdoNmM /视图?USP = drive_web。请改用Stack Overflow的内置图片上传功能。 –

+0

哦谢谢,我不知道。我在这里完全是新的。 – Vitja

+0

您是否使用任何API调用进行身份验证? – Miller

回答

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(); 
       } 
      } 
     }); 
    } 
} 
+0

谢谢,看起来很不错。我像你在这里写的那样做了它。第一点:我试过了,这个声明不适合我:Toast.makeText(this,“Eingestempelt”,Toast.LENGTH_SHORT).show(); ......它告诉我,我不能在主要活动中使用这个。第二点。我在哪里声明pref变量?并在哪一点上,我可以开始问服务器的登录数据? – Vitja

+0

多数民众赞成我按摩吐司按摩:无法解决方法'makeText(anoymous andoird.view.View.OnClickListener,java.lang.string,int'和在此:Intent意图=新的意图(这,MainActivity.class) ;我得到的按摩:无法解析方法'makeText(anoymous andoird.view.View.OnClickListener,java.lang.Class Vitja

+0

更改'Toast.makeText(this,“Eingestempelt”,Toast.LENGTH_SHORT).show() ;'to Toast.makeText(MainActivity.this,“Eingestempelt”,Toast.LENGTH_SHORT).show();' – Manitoba

相关问题