React Native学习(二)
上一小节讲了如何安装、创建项目,以及工程目录的一些基本概念。现在来看看代码。 public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage()
);
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.awesomeproject" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
<application android:name=".MainApplication" android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
<activity android:name=".LoginActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<activity android:name=".MainActivity" />
</application>
</manifest>
public class LoginActivity extends ReactActivity {
/** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. */
@Override
protected String getMainComponentName() {
return "LoginComponent";
}
}
MyApplication继承了ReactApplication,我还没看这里面的源码,但我猜想应该主要是做客户端连接服务器的工作吧(如果 有读者知道,请留言告知)。 /** * Sample React Native App * https://github.com/facebook/react-native * @flow */
import {
AppRegistry,} from 'react-native';
import LoginComponent from './component/login/LoginComponent';
AppRegistry.registerComponent('LoginComponent',() => LoginComponent);
在这个文件中,引入了与index.android.js同目录下的component文件下的login文件的LoginComponent.js文件
先导入loginComponent.js里面代码,然后用AppRegistry.registerComponent来注册,看到了么,这里注册的component就是LoginActivity指定的component。 import React,{ Component } from 'react';
import {
AppRegistry,StyleSheet,Text,View,Image,TextInput,TouchableHighlight,ToastAndroid,} from 'react-native';
export default class LoginComponent extends Component {
constructor(props){
super(props);
this.state={
account:'',psw:''
};
}
updateAccount(account) {
this.setState({
account: account,});
}
updatePsw(psw) {
this.setState({
psw: psw,});
}
goToNext(){
ToastAndroid.show(this.state.account+'and'+this.state.psw,ToastAndroid.LONG);
}
render() {
return (
<View style={styles.out_container}>
<Image
source={require('./icon_kf.png')}
style={styles.thumbnail}
/>
<View style={styles.inner_container}>
<Text style={styles.text}>账号:</Text>
<TextInput style={styles.textInput}
autoFocus={true}
onChangeText={(text) => this.updateAccount(text)}/>
</View>
<View style={styles.inner_container}>
<Text style={styles.text}>密码:</Text>
<TextInput style={styles.textInput}
onChangeText={(text) => this.setState({psw:text})}/>
</View>
<TouchableHighlight style={styles.button}
onPress = {()=>this.goToNext()}>
<Text style={styles.btnText}>下一步</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
out_container:{
flex: 1,justifyContent: 'center',alignItems: 'center',},inner_container: {
flexDirection: 'row',justifyContent: 'space-around',width:350,height:40,text: {
flex:1,fontSize: 20,marginRight: 10,textInput: {
flex:4,borderColor:'gray',button: {
width: 350,height: 40,marginTop:20,alignSelf: 'center',justifyContent:'center',backgroundColor: '#cab4af',btnText: {
fontSize: 18,alignSelf:'center',alignItems:'center'
},thumbnail:{
width:60,height:90,marginBottom:50,});
这段代码很长,运行起来的效果就是上节提到的页面:
所以我的总结是如果是导入widget(和原生app有关)这些的话,是从react-native中导入,而React和Component这些则是从react中导入。 讲完了这些就是创建一个具体的component。 export default class LoginComponent extends Component {
}
代码很简单,component和Activity类似,也是有生命周期的。 <TextInput style={styles.textInput}
autoFocus={true}
onChangeText={(text) => this.updateAccount(text)}/>
说到在一个Component中增加变量的话,就要用到state和props(状态和属性),可以看该篇文章:http://www.cnblogs.com/ZSG-DoBestMe/p/5293457.html constructor(props){ super(props); this.state={ account:'',psw:'' };
}
这样我们就为LoginComponent增加了account和psw两个变量了,用这两个来记录输入框中的内容。那如何修改呢,只要用setState方法,虽然这个方法我们没有在LoginComponent中声明,但是却可以直接调用,就是用来修改state中的变量的。例如我们要修改psw,可以这么写: this.setState({psw: newpsw});
newpsw就是我们要设的值。所以如果同时修改多个值,你可以封装出一个方法,例如: updateData(newAccount,newPsw) {
this.setState({
account: newAccount,psw:psw:newPsw });
}
接下来是讲讲Image,在代码中 <Image source={require('./icon_kf.png')} style={styles.thumbnail} />
加载的这张图片是与js目录同级的图片,而不是android中drawable目录下的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |